[英]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是否施加了相同的限制?
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
inApp\\Providers\\AppServiceProvider
viaregister
method.注意:我通过
register
方法在App\\Providers\\AppServiceProvider
注册了自己的RouteRegistrar
。
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
来处理此请求。
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
请求。
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.