Laravel-4:Laravel 中 RESTful 控制器和资源控制器的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22933301/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:19:38  来源:igfitidea点击:

Laravel-4: Difference between RESTful Controllers and Resource Controllers in Laravel

laravellaravel-4

提问by Asfaq Tamim

Someone can please explain what is the difference between RESTful Controllers and Resource Controllers in Laravel ? I also have some Questions-

有人可以解释一下 Laravel 中 RESTful 控制器和资源控制器之间的区别是什么?我也有一些问题——

when should I use RESTful Controllers and when Resource Controllers?

什么时候应该使用 RESTful 控制器,什么时候应该使用资源控制器?

  • Is there any naming convention Of Controller action for RESTful Controllers and Resource Controllers ?

  • If I use RESTful Controllers how could I define route for our controller ?

  • For building API which Controller Method is the best ?

  • RESTful 控制器和资源控制器的控制器操作是否有任何命名约定?

  • 如果我使用 RESTful 控制器,我如何为我们的控制器定义路由?

  • 对于构建 API,哪种控制器方法最好?

回答by majidarif

Laravel Resource Controllers are defined as Route::controller('users', 'UserController');while Restful Controllers are defined as Route::resource('photo', 'PhotoController');.

Laravel 资源控制器定义为,Route::controller('users', 'UserController');而 Restful 控制器定义为Route::resource('photo', 'PhotoController');.

A restful controllerfollows the standard blueprint for a restful resource which mainly consists of:

一个宁静的控制器遵循一个宁静资源的标准蓝图,主要包括:

GET         /resource                    index         resource.index
GET         /resource/create             create        resource.create
POST        /resource                    store         resource.store
GET         /resource/{resource}         show          resource.show
GET         /resource/{resource}/edit    edit          resource.edit
PUT/PATCH   /resource/{resource}         update        resource.update
DELETE      /resource/{resource}         destroy       resource.destroy

While the resource controllerisn't opinionated like the restful controller. It allows you to create methods directly from you controller and it all gets automatically mapped to your routes:

虽然资源控制器不像宁静的控制器那样固执己见。它允许您直接从控制器创建方法,并且所有方法都会自动映射到您的路由:

public function getIndex()
{
    // Route::get('/', 'Controller@getIndex');
}

public function postProfile()
{
    // Route::post('/profile', 'Controller@postProfile');
}

Will automatically have the routes like Route::post('/profile', 'Controller@postProfile');without explicitly defining it on the routes, much more of a helper if you will to avoid very long route files.

将自动拥有路由,Route::post('/profile', 'Controller@postProfile');而无需在路由上明确定义它,如果您要避免很长的路由文件,则它更像是一个助手。

Doing php artisan routeswill show you all your routes. You can test stuff out and use that command to see what routes gets automatically generated.

这样做php artisan routes会显示你所有的路线。您可以测试内容并使用该命令查看自动生成的路由。

回答by Andión

They are different concepts. In laravel, a resource controller defines all the default routes for a given named resource to follow REST principles.

它们是不同的概念。在 laravel 中,资源控制器为给定的命名资源定义了所有默认路由,以遵循REST 原则

So when you define a resource in your routes.php like:

因此,当您在 routes.php 中定义资源时,例如:

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

The only thing Laravel does is define for you this routes:

Laravel 唯一要做的就是为你定义这个路由:

Verb      Path                          Action  Route Name
GET       /resource                     index   resource.index
GET       /resource/create              create  resource.create
POST      /resource                     store   resource.store
GET       /resource/{resource}          show    resource.show
GET       /resource/{resource}/edit     edit    resource.edit
PUT/PATCH /resource/{resource}          update  resource.update
DELETE    /resource/{resource}          destroy resource.destroy

And expects that you define those methods on your controller. You can also use only/except clauses to remove unneeded routes:

并期望您在控制器上定义这些方法。您还可以使用 only/except 子句来删除不需要的路由:

Route::resource('user', 'UserController', ['except' => ['destroy']]);

More on this on Laravel's documentation.

有关Laravel 文档的更多信息

回答by Shawn McCool

It's just a distinction about the routing declaration. Instead of using one of those, manually define all of your routes.

这只是路由声明的一个区别。不要使用其中之一,而是手动定义所有路由。

Route::get(...);
Route::post(...);
Route::put(...);
Route::delete(...);
Route::patch(...);

It makes your routes file authoritative, easy to understand, and less buggy.

它使您的路由文件具有权威性、易于理解且错误更少。

回答by Phil

The documentation currently shows RESTful and Resource controllers to refer to the same thing.

该文档目前显示 RESTful 和 Resource 控制器指代同一事物。

Route::resource('resource', 'ResourceController');

It defines the routes for the followinghttp request verbsmapped to the URI, controller action, and route. This allows you to use the predefined route names to connect to predefined controller actions and will map resource_id to {resource} as shown.

它定义了映射到 URI、控制器操作和路由的以下http 请求动词的路由。这允许您使用预定义的路由名称连接到预定义的控制器操作,并将 resource_id 映射到 {resource},如图所示。

Verb      URI                                  Action  Route Name
GET       /resource/index.blade.php             index   resource
GET       /resource/create.blade.php            create  resource.create
POST      /resource                             store   resource.store
GET       /resource/{resource}/show.blade.php   show    resource.show
GET       /resource/{resource}/edit.blade.php   edit    resource.edit
PUT/PATCH                                       update  resource.update
DELETE                                          destroy resource.destroy

The term Implicit Controller seems to be the term to specify the use of

术语隐式控制器似乎是指定使用的术语

Route::controller('resource', 'ResourceController');

which will magically connect allroutes to to ResourceController so that the http request verb (get/post) is prefixed in the function name used in the controller. This maps any URI to the controller action (function) with (get/put) in front but does not map resource_id to {resource} or route names.

这将神奇地将所有路由连接到 ResourceController,以便 http 请求动词 (get/post) 以控制器中使用的函数名称为前缀。这将任何 URI 映射到控制器操作(函数),前面带有 (get/put),但不会将 resource_id 映射到 {resource} 或路由名称。

class UserController extends BaseController {

    public function getIndex()
    {
        //
    }

    public function postProfile()
    {
        //
    }

    public function anyLogin()
    {
        //
    }
}

maps to

映射到

Verb      URI                  Action           Route Name
GET       /index               getIndex         
POST      /profile             postProfile      
GET       /login               anyLogin         
POST      /login               anyLogin         
DELETE    /login               anyLogin         

It's up to you to decide which method to use if any for routing. There is some debate as to what is useful and if routing is even worth the confusion it can cause.

由您决定使用哪种方法进行路由。关于什么是有用的以及路由是否值得它可能引起的混乱存在一些争论。

回答by Mansukh Ahir

RESTful Resource Controllers

RESTful 资源控制器

Resource controllers make it easier to build RESTful controllers around resources. For example, you may wish to create a controller that manages "photos" stored by your application. Using the controller:make command via the Artisan CLI and the Route::resource method, we can quickly create such a controller.

资源控制器使围绕资源构建 RESTful 控制器变得更加容易。例如,您可能希望创建一个控制器来管理应用程序存储的“照片”。通过 Artisan CLI 和 Route::resource 方法使用 controller:make 命令,我们可以快速创建这样的控制器。

To create the controller via the command line, execute the following command:

要通过命令行创建控制器,请执行以下命令:

php artisan controller:make PhotoController

Now we can register a resourceful route to the controller:

现在我们可以向控制器注册一个资源丰富的路由:

Route::resource('photo', 'PhotoController');

This single route declaration creates multiple routes to handle a variety of RESTful actions on the photo resource. Likewise, the generated controller will already have stubbed methods for each of these actions with notes informing you which URIs and verbs they handle.

这个单一的路由声明创建了多个路由来处理照片资源上的各种 RESTful 操作。同样,生成的控制器已经为这些操作中的每一个提供了存根方法,并带有注释,通知您它们处理哪些 URI 和动词。

Actions Handled By Resource Controller

资源控制器处理的操作

http://laravel.com/docs/5.0/controllers#restful-resource-controllers

http://laravel.com/docs/5.0/controllers#restful-resource-controllers