Laravel - Route::resource vs Route::controller
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23505875/
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 - Route::resource vs Route::controller
提问by Sonique
I read the docs on the Laravel website, Stack Overflow, and Google but still don't understand the difference between Route::resource
and Route::controller
.
我阅读文档的Laravel网站,堆栈溢出,并且谷歌,但还是不明白之间的差别Route::resource
和Route::controller
。
One of the answers said Route::resource was for crud. However, with Route::controller we can accomplish the same thing as with Route::resource and we can specify only the needed actions.
其中一个答案说 Route::resource 是为了 crud。然而,使用 Route::controller 我们可以完成与使用 Route::resource 相同的事情,我们可以只指定所需的操作。
They appear to be like siblings:
他们看起来像兄弟姐妹:
Route::controller('post','PostController');
Route::resource('post','PostController');
How we can choose what to use? What is good practice?
我们如何选择使用什么?什么是好的做法?
回答by ryanwinchester
RESTful Resource controller
RESTful 资源控制器
A RESTful resource controllersets up some default routes for you and even names them.
一个RESTful的资源控制器为您设置,甚至名称他们一些缺省路由。
Route::resource('users', 'UsersController');
Gives you these named routes:
为您提供这些命名路由:
Verb Path Action Route Name
GET /users index users.index
GET /users/create create users.create
POST /users store users.store
GET /users/{user} show users.show
GET /users/{user}/edit edit users.edit
PUT|PATCH /users/{user} update users.update
DELETE /users/{user} destroy users.destroy
And you would set up your controller something like this (actions = methods)
你会像这样设置你的控制器(动作=方法)
class UsersController extends BaseController {
public function index() {}
public function show($id) {}
public function store() {}
}
You can also choose what actions are included or excluded like this:
您还可以选择包含或排除的操作,如下所示:
Route::resource('users', 'UsersController', [
'only' => ['index', 'show']
]);
Route::resource('monkeys', 'MonkeysController', [
'except' => ['edit', 'create']
]);
RESTful Resource Controller documentation
Implicit controller
隐式控制器
An Implicit controlleris more flexible. You get routed to your controller methods based on the HTTP request type and name. However, you don't have route names defined for you and it will catch all subfolders for the same route.
的隐式控制器是更灵活的。您将根据 HTTP 请求类型和名称路由到您的控制器方法。但是,您没有为您定义路由名称,它会捕获同一路由的所有子文件夹。
Route::controller('users', 'UserController');
Would lead you to set up the controller with a sort of RESTful naming scheme:
会引导您使用一种 RESTful 命名方案来设置控制器:
class UserController extends BaseController {
public function getIndex()
{
// GET request to index
}
public function getShow($id)
{
// get request to 'users/show/{id}'
}
public function postStore()
{
// POST request to 'users/store'
}
}
Implicit Controller documentation
It is good practice to use what you need, as per your preference. I personally don't like the Implicit controllers, because they can be messy, don't provide names and can be confusing when using php artisan routes
. I typically use RESTful Resource controllersin combination with explicit routes.
根据您的喜好使用您需要的东西是一种很好的做法。我个人不喜欢隐式控制器,因为它们可能很混乱,不提供名称并且在使用php artisan routes
. 我通常将RESTful 资源控制器与显式路由结合使用。
回答by Ahmad Sharif
For route controller method we have to define only one route. In get or post method we have to define the route separately.
对于路由控制器方法,我们只需要定义一个路由。在 get 或 post 方法中,我们必须单独定义路由。
And the resources method is used to creates multiple routes to handle a variety of Restful actions.
并且resources方法用于创建多个路由来处理各种Restful动作。
Here the Laravel documentationabout this.
这里有关于这个的 Laravel文档。