Laravel-Route :: resource vs Route :: controller [英] Laravel - Route::resource vs Route::controller

查看:77
本文介绍了Laravel-Route :: resource vs Route :: controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了Laravel网站,Stack Overflow和Google上的文档,但仍然不了解Route::resourceRoute::controller之间的区别.

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.

答案之一是Route :: resource是为粗野而生的.但是,使用Route :: controller可以完成与Route :: resource相同的操作,并且只能指定所需的操作.

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.

它们看起来像兄弟姐妹:

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?

推荐答案

RESTful资源控制器

RESTful资源控制器为您设置了一些默认路由,甚至为它们命名.

RESTful Resource controller

A RESTful resource controller sets up some default routes for you and even names them.

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

然后您将像这样设置控制器(actions =方法)

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资源控制器文档


隐式控制器更加灵活.您将基于HTTP请求类型和名称被路由到控制器方法.但是,您没有为您定义的路由名称,它将捕获同一路由的所有子文件夹.

An Implicit controller is 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.

Route::controller('users', 'UserController');

将带您使用一种RESTful命名方案来设置控制器:

Would lead you to set up the controller with a sort of RESTful naming scheme:

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'
    }

}

隐式控制器文档


根据您的喜好使用所需的东西是一个好习惯.我个人不喜欢隐式控制器,因为它们可能很杂乱,不提供名称,并且在使用php artisan routes时会造成混淆.我通常将 RESTful资源控制器与显式路由结合使用.

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 controllers in combination with explicit routes.

这篇关于Laravel-Route :: resource vs Route :: controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆