路由感到困惑,我们为控制器写了一些路由。如:我对route.php中的laravel
Route::post('/account/create',array(
'as' =>'account-create',
'uses'=>'[email protected]'
));
我知道“使用”是搜索控制器,那么'as'处理什么?
路由感到困惑,我们为控制器写了一些路由。如:我对route.php中的laravel
Route::post('/account/create',array(
'as' =>'account-create',
'uses'=>'[email protected]'
));
我知道“使用”是搜索控制器,那么'as'处理什么?
as
用于在laravel中创建named-route
。
由于the doc says
:
我们可以用它们来指代路线而生成的URL重定向或:
//generate URL
$url = URL::route('account-create');
//redirect to the route from another
$redirect = Redirect::route('account-create');
// with helpers
$url = route('account-create');
$redirect = redirect()->route('account-create');
as
是用于创建命名的路线,实际上这是非常有用的。在您的应用程序中,您可以使用此命名路线URL::route('account-create');
和Redirect::route('account-create');
创建网址或重定向,它可以为您提供巨大的奖励。
如果您决定要更改网址,只需在路线中更改它,并且所有内容都可以正常工作,因为在其他地方您只使用路由名称。
因此,例如,如果使用命名路线:
Route::post('/account/create',array(
'as' =>'account-create',
'uses'=>'[email protected]'
));
,并在其他地区URL::route('account-create');
和Redirect::route('account-create');
,现在你决定要改变URL从/account/create
到newaccount
你只需要在routes.php
/account/create
改变newaccount
并且您的整个应用程序将毫无问题地工作
相反,如果您在应用程序的其他部分使用了urls,如果要更改此路由网址,则需要更改m你的应用程序的任何部分,所以你应该使用命名路由,因为如果你决定改变一些网址将来可以节省大量的时间。
举例来说,如果你不使用命名路线:
Route::post('/account/create',array(
'uses'=>'[email protected]'
));
,并在其他地区URL::to('/account/create');
和Redirect::to('/account/create');
,现在你决定从/account/create
您的网址更改为newaccount
你不仅需要改变在routes.php
文件中,但在所有使用此URL的文件中,您可以在此制作任何URL::to('/account/create');
或Redirect::to('/account/create');
非常感谢。我明白了 – tigerDisplayName 2014-09-25 12:20:10
谢谢!似乎使用'as'为我们提供了访问路线的简单方法,并且它不会显示在网址上。 – tigerDisplayName 2014-09-25 08:20:03
@tigerDisplayName:在应用程序代码中引用您的路线的简单方法。基本上它服务于别名的目的。 – mithunsatheesh 2014-09-25 08:20:53