【问题标题】:I am confused about routing in laravel我对laravel中的路由感到困惑
【发布时间】:2014-11-19 22:15:34
【问题描述】:

在 routes.php 中,我们为控制器编写了一些路由。如:

Route::post('/account/create',array(
    'as' =>'account-create',
    'uses'=>'AccountController@postCreate'
));

我知道'uses'是搜索控制器,那么'as'处理的是什么?

【问题讨论】:

    标签: php laravel laravel-4 laravel-routing laravel-5


    【解决方案1】:

    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”为我们提供了一种访问路线的简便方法。它不会显示在网址上。
    • @tigerDisplayName :在应用程序代码中引用您的路线的简单方法。基本上它服务于别名的目的。
    【解决方案2】:

    as 用于创建命名路由,实际上它非常有用。在您的应用程序中,您可以使用此命名路由 URL::route('account-create');Redirect::route('account-create'); 创建 url 或重定向,它会给您带来巨大的好处。

    如果您决定要更改您的 url,您只需在您的路由中更改它,一切都会顺利进行,因为在其他部分您只使用了路由名称。

    例如,如果您使用命名路由:

    Route::post('/account/create',array(
        'as' =>'account-create',
        'uses'=>'AccountController@postCreate'
    ));
    

    在其他部分 URL::route('account-create');Redirect::route('account-create'); 现在您决定要将 url 从 /account/create 更改为 newaccount 您只需将 routes.php /account/create 更改为 newaccount 和您的全部应用程序将毫无问题地运行

    相反,如果您在应用程序的其他部分使用 url,如果您想更改此路由 url,则需要在应用程序的许多部分更改 url,因此您应该使用命名路由,因为它可以为您节省大量资金如果您决定将来更改一些 url,是时候。

    例如,如果您不使用命名路由:

    Route::post('/account/create',array(       
        'uses'=>'AccountController@postCreate'
    ));
    

    在其他部分 URL::to('/account/create');Redirect::to('/account/create'); 现在您决定将您的网址从 /account/create 更改为 newaccount 您不仅需要在 routes.php 文件中更改它,而且在所有使用此文件的文件中都需要更改它您在其中创建任何URL::to('/account/create');Redirect::to('/account/create'); 的网址

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      • 2016-11-18
      相关资源
      最近更新 更多