我敢肯定这很简单,但我不明白。请解释一下,来自 documentation :
Naming Controller Routes Like Closure routes, you may specify names on controller routes:
Route::get('foo', ['uses' => 'FooController@method', 'as' => 'name']);
我不清楚“uses”、“as”和“name”的目的,以及如何使用它们。
更新:
所以我将路由闭包命名为 'bar'
但在尝试调用 /bar
或 时出现了
在 URL 中NotFoundHttpException
/qux
Route::get('foo', ['as' => 'bar', function() {
dd('foo');
}]);
Route::get('qux', function() {
action('bar');
});
最佳答案
uses
指定您在路由命中时调用哪个类,以及该类的哪个方法。因此,对于 Laravel 5.1,以您的示例为例,默认情况下是类 app/Http/Controllers/FooController.php
method()
as
表示您为路由命名,以便以后更容易链接到它。假设您有十个 View ,并且您从所有 View 链接到同一条路线。如果您更改链接的格式,则必须单独跟踪每个链接并进行更改。但是,如果您已为路线命名,则无需更改任何内容,因为它们只会按该名称查找路线。
综上所述,假设您有一个网上商店,您想要一个客户页面的路径,并且您想要从您的 View 链接到它。它可能看起来像这样:
Route::get('customers', [
'uses' => 'ShopController@customers',
'as' => 'customers'
]);
关于php - Laravel - 请解释命名 Controller 路由 'uses' , 'as',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34123107/