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

查看:22
本文介绍了Laravel - Route::resource 与 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 是用于 crud.但是,使用 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

你会像这样设置你的控制器(动作=方法)

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']
]);

API 资源控制器

Laravel 5.5 添加了另一种处理资源控制器路由的方法.API 资源控制器 的行为与上图完全一样,但不注册 createedit 路由.它旨在用于简化 RESTful API 中使用的映射路由 - 通常在 createedit 方法中没有任何类型的数据.

API Resource controller

Laravel 5.5 added another method for dealing with routes for resource controllers. API Resource Controller acts exactly like shown above, but does not register create and edit routes. It is meant to be used for ease of mapping routes used in RESTful APIs - where you typically do not have any kind of data located in create nor edit methods.

Route::apiResource('users', 'UsersController');

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 与 Route::controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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