如何覆盖laravel资源路由默认方法?

时间:2016-09-09 08:22:26

标签: php rest laravel

我的REST API网址有这个架构:

Verb    Url                         Method

GET     /tasks                      findAll   
GET     /tasks/{id}                 findOne    
POST    /tasks                      create   
PUT     /tasks/{id}                 update    
DELETE  /tasks/{id}                 deleteOne
DELETE  /tasks                      deleteAll

有没有办法覆盖Route Resource Laravel内置方法的默认方法(存储,创建,编辑等...),并用一行创建与我的控制器关联的自定义路由?

例如:

Route::resource('/tasks', 'TasksController');

而不是:

Route::get('/tasks', 'TasksController@findAll');
Route::get('/tasks/{id}', 'TasksController@findOne');
Route::post('/tasks', 'TasksController@create');
Route::put('/tasks/{id}', 'TasksController@update');
Route::delete('/tasks', 'TasksController@deleteAll');
Route::delete('/tasks/{id}', 'TasksController@deleteOne');

2 个答案:

答案 0 :(得分:2)

我已经解决了这些步骤更改ResourceRegistrar.php类,这实现了我的请求。 (由@Thomas Van der Veen建议):

1)我用我的欲望方法替换了$ resourceDefaults数组:

protected $resourceDefaults = ['findAll', 'findOne', 'create', 'update', 'deleteOne', 'deleteAll'];

2)在创建执行操作的方法后,删除olders。

    protected function addResourceFindAll($name, $base, $controller, $options)
{
    $uri = $this->getResourceUri($name);

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

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

protected function addResourceFindOne($name, $base, $controller, $options)
{
    $uri = $this->getResourceUri($name).'/{'.$base.'}';

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

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

protected function addResourceCreate($name, $base, $controller, $options)
{
    $uri = $this->getResourceUri($name);

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

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

protected function addResourceUpdate($name, $base, $controller, $options)
{
    $uri = $this->getResourceUri($name).'/{'.$base.'}';

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

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

protected function addResourceDeleteAll($name, $base, $controller, $options)
{
    $uri = $this->getResourceUri($name);

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

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

protected function addResourceDeleteOne($name, $base, $controller, $options)
{
    $uri = $this->getResourceUri($name).'/{'.$base.'}';

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

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

就是这样,效果很好!

答案 1 :(得分:1)

您应该在这里查看Laravel文档:https://laravel.com/docs/5.3/controllers#resource-controllers

关于deleteAll操作,Laravel不提供默认调用。 我建议避免这种情况。

为了创建资源控制器,只需在终端上输入以下命令:

php artisan make:controller TasksController --resource

这将创建一个控制器,其中包含您必须填写的默认CRUD操作。

然后在routes.php中添加:

Route::resource('tasks', 'TasksController');

您将能够像您所描述的那样向服务器发送呼叫。