2014-09-25 37 views

回答

4

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'); 
+0

谢谢!似乎使用'as'为我们提供了访问路线的简单方法,并且它不会显示在网址上。 – tigerDisplayName 2014-09-25 08:20:03

+1

@tigerDisplayName:在应用程序代码中引用您的路线的简单方法。基本上它服务于别名的目的。 – mithunsatheesh 2014-09-25 08:20:53

0

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/createnewaccount你只需要在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');

+0

非常感谢。我明白了 – tigerDisplayName 2014-09-25 12:20:10

相关问题