简体   繁体   English

Laravel 4和5中的RESTful和资源控制器

[英]RESTful and Resource Controllers in Laravel 4 and 5

RESTful and Resource controllers in Laravel 4 are limited that is RESTful method names has to end with get,put,post,patch,delete and Resource controllers has to end with index, create, store,show edit,update,destroy. Laravel 4中的RESTful和Resource控制器受到限制,因为RESTful方法名称必须以get,put,post,patch,delete结尾,而Resource控制器必须以索引,创建,存储,显示编辑,更新,销毁结尾。 My question is does Laravel 5 impose the same restrictions? 我的问题是Laravel 5是否施加了相同的限制?

Preface 前言

Natively, Yes, it does . 本机, 是的 Read here . 在这里阅读。 But if you want something different, I give you a trick to do this. 但是,如果您想要不同的东西,我会给您一个窍门。 First, you may create your own ResourceRegistrar . 首先,您可以创建自己的ResourceRegistrar Mine is [located in app/Routing/ResourceRegistrar.php ]: 我的位于[位于app/Routing/ResourceRegistrar.php ]:

namespace App\Routing;

use Illuminate\Routing\ResourceRegistrar as BaseRegistrar;

class ResourceRegistrar extends BaseRegistrar
{
}

Then register your own RouteRegistrar in your service provider: 然后在服务提供商中注册自己的RouteRegistrar

$this->app->bind('Illuminate\Routing\ResourceRegistrar', 'App\Routing\ResourceRegistrar');

Notes: I register my own RouteRegistrar in App\\Providers\\AppServiceProvider via register method. 注意:我通过register方法在App\\Providers\\AppServiceProvider注册了自己的RouteRegistrar

Example

I add my own resource controller in routes.php something like this: 我在routes.php添加了自己的资源控制器,如下所示:

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

So, I should have a PhotoController to handle this request. 因此,我应该有一个PhotoController来处理此请求。

Implementation 履行

We know, that a GET request to '/photo' will be handled by PhotoController@index method, to modify your photo:index action to photo:root action, modify your ResourceRegistrar to something like this: 我们知道,对“ / photo”的GET请求将由PhotoController@index方法处理,将您的photo:index操作修改为photo:root操作,将ResourceRegistrar修改为如下所示:

namespace App\Routing;

use Illuminate\Routing\ResourceRegistrar as BaseRegistrar;

class ResourceRegistrar extends BaseRegistrar
{
    protected function addResourceIndex($name, $base, $controller, $options)
    {
        $uri = $this->getResourceUri($name);

        $action = $this->getResourceAction($name, $controller, 'root', $options);

        return $this->router->get($uri, $action);
    }
}

So now GET request to '/photo' will be handled by PhotoController@root method. 因此,现在将通过PhotoController@root方法处理对“ / photo”的GET请求。

Cheat Sheet 备忘单

Verb      | Path                  | Method to modify  |
----------|-----------------------|-----------------  |
GET       | `/photo`              | addResourceIndex  |
GET       | `/photo/create`       | addResourceCreate |
POST      | `/photo`              | addResourceStore  |
GET       | `/photo/{photo}`      | addResourceShow   |
GET       | `/photo/{photo}/edit` | addResourceEdit   |
PUT/PATCH | `/photo/{photo}`      | addResourceUpdate |
DELETE    | `/photo/{photo}`      | addResourceDestroy|

See the base code of ResourceRegistrar here . 请在此处查看ResourceRegistrar的基本代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM