未找到Laravel RESTful控制器方法

时间:2013-03-21 21:05:21

标签: routes laravel restful-url laravel-4

我无法让我的控制器收听DELETE请求。当我向/ api / players / 1发送DELETE请求时,我得到一个NotFoundHttpException。这是我的routes.php

Route::group(array('prefix' => 'api'), function() {

    Route::controller('matches', 'MatchController');
    Route::controller('players', 'PlayerController');
    Route::controller('auth', 'AuthController');

});

我的PlayerController

class PlayerController extends BaseController {

    public function getIndex() {
        // do something... this works
    }

    public function postIndex() {
        // do something... this works
    }

    public function deleteIndex() {
        // works when I send a DELETE request to /api/players
    }

    public function delete($id = null) {
        // doesn't work!
    }
}

我想我错过了某种秘密词或什么......我只是不知道如何捕获带有id的DELETE请求。我做错了什么?

1 个答案:

答案 0 :(得分:1)

好的......所以现在我已经将我的控制器更改为资源控制器...我不完全确定这是否是正确的方法,但似乎有效!

新routes.php:

Route::group(array('prefix' => 'api'), function() {

    Route::resource('matches', 'MatchController');
    Route::resource('players', 'PlayerController');
    Route::resource('auth', 'AuthController');

});

PlayerController.php:

class PlayerController extends BaseController {

    public function index() {
        // GET /api/players
    }

    public function store() {
        // POST /api/players
    }

    public function destroy($id) {
        // DELETE /api/players/{id}
    }
}