Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel4和5中的RESTful和资源控制器_Php_Laravel_Laravel 4_Laravel 5 - Fatal编程技术网

Php Laravel4和5中的RESTful和资源控制器

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

Laravel 4中的RESTful和资源控制器受到限制,即RESTful方法名称必须以get、put、post、patch、delete结尾,而资源控制器必须以index、create、store、show edit、update和destroy结尾。我的问题是,Laravel 5是否施加了相同的限制?

前言 就本机而言,是的,确实如此。阅读但是如果你想要不同的东西,我给你一个窍门。首先,您可以创建自己的
资源注册器
。我的网站[位于
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也没有任何限制。您可以根据需要添加任意数量的路线。Laravel
route::resource
仅用于帮助您生成更清晰的代码,而无需键入每个代码并根据一般API需求生成。