Php Laravel4和5中的RESTful和资源控制器
Laravel 4中的RESTful和资源控制器受到限制,即RESTful方法名称必须以get、put、post、patch、delete结尾,而资源控制器必须以index、create、store、show edit、update和destroy结尾。我的问题是,Laravel 5是否施加了相同的限制?前言 就本机而言,是的,确实如此。阅读但是如果你想要不同的东西,我给你一个窍门。首先,您可以创建自己的Php Laravel4和5中的RESTful和资源控制器,php,laravel,laravel-4,laravel-5,Php,Laravel,Laravel 4,Laravel 5,Laravel 4中的RESTful和资源控制器受到限制,即RESTful方法名称必须以get、put、post、patch、delete结尾,而资源控制器必须以index、create、store、show edit、update和destroy结尾。我的问题是,Laravel 5是否施加了相同的限制?前言 就本机而言,是的,确实如此。阅读但是如果你想要不同的东西,我给你一个窍门。首先,您可以创建自己的资源注册器。我的网站[位于app/Routing/resourceRegistrator.p
资源注册器
。我的网站[位于app/Routing/resourceRegistrator.php
]:
namespace App\Routing;
use Illuminate\Routing\ResourceRegistrar as BaseRegistrar;
class ResourceRegistrar extends BaseRegistrar
{
}
然后在您的服务提供商中注册您自己的RouterRegistrar
:
$this->app->bind('Illuminate\Routing\ResourceRegistrar', 'App\Routing\ResourceRegistrar');
注意:我通过register
方法在App\Providers\AppServiceProvider
中注册我自己的routerregister
例子
我在routes.php
中添加了自己的资源控制器,如下所示:
Route::resource('photo', 'PhotoController');
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);
}
}
所以,我应该有一个光控制器来处理这个请求
实施
我们知道,GET
对“/photo”的请求将由PhotoController@index
方法,要将photo:index
操作修改为photo:root
操作,请将resourceRegistrator
修改为如下内容:
Route::resource('photo', 'PhotoController');
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);
}
}
所以现在,GET
对“/photo”的请求将由PhotoController@root
方法
备忘单
请参阅ResourceRegistrator的基本代码
即使在laravel 4也没有任何限制。您可以根据需要添加任意数量的路线。Laravelroute::resource
仅用于帮助您生成更清晰的代码,而无需键入每个代码并根据一般API需求生成。