php 资源控制器的 Laravel 命名路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25290229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel named route for resource controller
提问by flyingL123
Using Laravel 4.2, is it possible to assign a name to a resource controller route? My route is defined as follows:
使用 Laravel 4.2,是否可以为资源控制器路由分配名称?我的路线定义如下:
Route::resource('faq', 'ProductFaqController');
I tried adding a name option to the route like this:
我尝试向路线添加名称选项,如下所示:
Route::resource('faq', 'ProductFaqController', array("as"=>"faq"));
However, when I hit the /faq route and place {{ Route::currentRouteName() }}
in my view, it yields faq.faq.index
instead of just faq
.
但是,当我点击 /faq 路线并放置{{ Route::currentRouteName() }}
在我的视图中时,它会产生faq.faq.index
而不仅仅是faq
.
回答by Aken Roberts
When you use a resource controller route, it automatically generates names for each individual route that it creates. Route::resource()
is basically a helper method that then generates individual routes for you, rather than you needing to define each route manually.
当您使用资源控制器路由时,它会自动为它创建的每个单独路由生成名称。Route::resource()
基本上是一个辅助方法,然后为您生成单独的路由,而不是您需要手动定义每个路由。
You can view the route names generated by typing php artisan routes
in Laravel 4 or php artisan route:list
in Laravel 5 into your terminal/console. You can also see the types of route names generated on the resource controller docs page (Laravel 4.x| Laravel 5.x).
您可以查看通过php artisan routes
在php artisan route:list
终端/控制台中输入Laravel 4 或Laravel 5生成的路由名称。您还可以在资源控制器文档页面 ( Laravel 4.x| Laravel 5.x)上查看生成的路由名称类型。
There are two ways you can modify the route names generated by a resource controller:
有两种方法可以修改资源控制器生成的路由名称:
Supply a
names
array as part of the third parameter$options
array, with each key being the resource controller method (index, store, edit, etc.), and the value being the name you want to give the route.Route::resource('faq', 'ProductFaqController', [ 'names' => [ 'index' => 'faq', 'store' => 'faq.new', // etc... ] ]);
Specify the
as
option to define a prefix for every route name.Route::resource('faq', 'ProductFaqController', [ 'as' => 'prefix' ]);
This will give you routes such as
prefix.faq.index
,prefix.faq.store
, etc.
提供一个
names
数组作为第三个参数$options
数组的一部分,每个键是资源控制器方法(索引、存储、编辑等),值是您要为路由指定的名称。Route::resource('faq', 'ProductFaqController', [ 'names' => [ 'index' => 'faq', 'store' => 'faq.new', // etc... ] ]);
指定
as
为每个路由名称定义前缀的选项。Route::resource('faq', 'ProductFaqController', [ 'as' => 'prefix' ]);
这会给你的路线,例如
prefix.faq.index
,prefix.faq.store
等等。
回答by Marc
For answer seekers with Laravel 5.5+ finding this page:
对于使用 Laravel 5.5+ 查找此页面的答案寻求者:
Route::namespace('Admin')->prefix('admin')->name('admin.')->group(function () {
Route::resource('users','UserController');
});
These options will result in the following for the Resource:
这些选项将为Resource产生以下结果:
namespace()
sets Controller namespace to \Admin\UserControllerprefix()
sets request URi to /admin/usersname()
sets route name accessor toroute('admin.users.index')
namespace()
将控制器命名空间设置为 \Admin\UserControllerprefix()
将请求 URI 设置为 /admin/usersname()
将路由名称访问器设置为route('admin.users.index')
In
name()
the DOT is intended, it is not a typo.
在
name()
DOT 中,它不是一个错字。
Please let others know if this works in comments for any versions prior to Laravel 5.5, I will update my answer.
请让其他人知道这是否适用于 Laravel 5.5 之前的任何版本的评论,我会更新我的答案。
Update:
更新:
I can confirm that in Laravel 5.3that the name
method is not available.
No confirmation yet if supported in 5.4
我可以确认在 Laravel 5.3中该name
方法不可用。如果5.4支持还没有确认
Taylor accepted my PR to officially document this in 5.5:
Taylor 接受了我的 PR 在 5.5 中正式记录了这一点:
https://laravel.com/docs/5.5/routing#route-group-name-prefixes
https://laravel.com/docs/5.5/routing#route-group-name-prefixes
回答by Hamidreza
I don't know if it's available in laravel 4.2 (I tested in 5.7) but you can use names
to change the name of all routes generated by resource
我不知道它在 laravel 4.2 中是否可用(我在 5.7 中测试过)但您可以使用它names
来更改资源生成的所有路由的名称
Route::resource('faq', 'ProductFaqController', ['names' => 'something']);
and the result will be like this
结果会是这样
something.index
and you don't need to specify each route
并且您不需要指定每条路线
回答by Brayan Angarita
Using Laravel 5.5
使用 Laravel 5.5
Route::resource('admin/posts/tags', 'PostTagController', ['as' => 'posts']);
Route::resource('admin/posts/tags', 'PostTagController', ['as' => 'posts']);
important to keep in mind the "resource"
重要的是要记住“资源”
For example, I send something from my project:
例如,我从我的项目中发送了一些东西:
Route::resource('admin/posts/tags', 'PostTagController', ['as' => 'posts']);
回答by Uzair
All Updates Later then Laravel 5.5 Using
Laravel 5.5 之后的所有更新使用
Route::resource('faqs', 'FaqController', ['as' => 'faqs']);
if we not use ['as' => 'faqs']
in above code then it will also work same.
如果我们不在['as' => 'faqs']
上面的代码中使用,那么它也将工作相同。
[Updated]
[更新]
Important to keep in mind that this will work for "resource"
重要的是要记住,这将适用于“资源”
For example:
例如:
Route::resource('admin/posts/tags', 'PostTagController', ['as' => 'posts']);
and result will be like
结果会像
POST | admin/posts/tags | posts.tags.store
GET|HEAD | admin/posts/tags | posts.tags.index
GET|HEAD | admin/posts/tags/create | posts.tags.create
DELETE | admin/posts/tags/{tag} | posts.tags.destroy
PUT|PATCH | admin/posts/tags/{tag} | posts.tags.update
GET|HEAD | admin/posts/tags/{tag} | posts.tags.show
GET|HEAD | admin/posts/tags/{tag}/edit | posts.tags.edit
回答by Omid Ahmadyani
you dont need to set name in resource in laravel 5.7 that i have test it. it auto generate route name from url.
您不需要在我测试过的 laravel 5.7 中的资源中设置名称。它从 url 自动生成路由名称。