php Laravel 4 - Route::resource 与 Route::controller。使用哪个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19102534/
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 4 - Route::resource vs Route::controller. Which to use?
提问by Gravy
I understand that a Resource controller can have the following methods
我知道资源控制器可以有以下方法
index
show
create
edit
store
update
destroy
Now suppose I have the following actions which need to be performed in addition to the resource actions:
现在假设除了资源操作之外,我还需要执行以下操作:
- User attempts to log in.
- Admin wishes to find a user by email / first-name
- User requests a post by it's slug
- 用户尝试登录。
- 管理员希望通过电子邮件/名字查找用户
- 用户通过它的 slug 请求帖子
Are resource controllers useless for the above functionality? If programming an API, I obviously want the index, show, edit,create,destroy... but also the login, find, search etc...
资源控制器对上述功能无用吗?如果对 API 进行编程,我显然想要索引、显示、编辑、创建、销毁……以及登录、查找、搜索等……
Is it possible to route to both types of controller? e.g.
是否可以路由到两种类型的控制器?例如
Route::group(['prefix' => 'api'], function() {
Route::group(['prefix' => 'v1'], function() {
// Resource Controller
Route::resource('posts', 'Api\V1\PostsResourceController');
// Restful Controller
Route::controller('posts', 'Api\V1\PostsController');
});
});
Or should I just forget about the resource controller and use a restful controller instead?
或者我应该忘记资源控制器并改用宁静的控制器?
回答by Joseph Silber
Just use a resource controller, add those other methods to that same controller, and add routes to those methods directly:
只需使用资源控制器,将其他方法添加到同一个控制器,然后直接向这些方法添加路由:
Route::group(['prefix' => 'api'], function()
{
Route::group(['prefix' => 'v1', 'namespace' => 'Api\V1'], function()
{
// Add as many routes as you need...
Route::post('login', 'PostsResourceController@login');
Route::get('find', 'PostsResourceController@find');
Route::get('search', 'PostsResourceController@search');
Route::resource('posts', 'PostsResourceController');
});
});
P.S.I generally shy away from using Route::controller()
. It's too ambiguous.
PS我通常回避使用Route::controller()
. 太暧昧了。
回答by Sojan V Jose
one of the problems associated with resource controllers are when you are using named routes, with group prefixes it all turns out into a big mess . if you want to make a small change in your prefix, you have to make changes throughout the views and controllers . ie you can't make full power of named routes.
与资源控制器相关的问题之一是,当您使用命名路由时,如果使用组前缀,一切都会变得一团糟。如果你想对你的前缀做一个小的改变,你必须在整个视图和控制器中进行改变。即你不能充分利用命名路线。
i follow this model when developing my laravel apps .
我在开发我的 Laravel 应用程序时遵循这个模型。
Route::group( [ 'prefix' => 'admin' ], function(){
Route::resource('pages', 'PageController', [
'names' => [
'show' => 'page',
'edit' => 'page.edit'
],
'only' => [
'show',
'edit'
]
]);
});
so that i have the following advantages .
使我有以下优点。
- there are only routes that you need.
- all the urls are clearly named
- 只有您需要的路线。
- 所有的网址都有明确的名称
and i can generate urls comfortably using the syntax,even if i make a change in prefix or resource names urls are not affected
并且我可以使用语法轻松地生成 url,即使我更改前缀或资源名称 url 也不受影响
URL::route('page', array($id))