Laravel:资源更新MethodNotAllowedHttpException

时间:2017-03-20 09:22:59

标签: php laravel-5 laravel-5.3

我看到一些人有同样的问题,但没有一个给定的回答可以帮助我。

我目前正在学习如何使用Laravel 5.3,但我遇到资源问题。我在api.php中创建了以下资源:

Route::resource('game','GameController',['except' => [
    'create','edit'
]]);

所以当我调用url laravel.dev/api/game/3一切正常时,我的游戏格式化为JSON:

{
   "type": {
      "id": 3,
      "name": "MMO"
    },
    "id": 2,
    "name": "World of Warcraft"
}

与DELETE请求相同,一切正常。

但是当我尝试使用PUT或PATCH请求更新我的游戏时,我得到以下异常:MethodNotAllowedHttpException

以下是一些可能对您有所帮助的信息:

php artisan route:list命令的输出:

POST      | api/game        | game.store   | App\Http\Controllers\GameController@store               
GET|HEAD  | api/game        | game.index   | App\Http\Controllers\GameController@index
GET|HEAD  | api/game/{game} | game.show    | App\Http\Controllers\GameController@show
PUT|PATCH | api/game/{game} | game.update  | App\Http\Controllers\GameController@update
DELETE    | api/game/{game} | game.destroy | App\Http\Controllers\GameController@destroy

用于发出PUT请求的代码:

<?php

$client = new http\Client;
$request = new http\Client\Request;

$body = new http\Message\Body;
$body->addForm(array(
    'name' => 'Test update'
), NULL);

$request->setRequestUrl('http://laravel.dev/api/game/3');
$request->setRequestMethod('PUT');
$request->setBody($body);

$request->setHeaders(array(
    'postman-token' => 'f674324c-936b-58bc-606e-98c085f5e851',
    'cache-control' => 'no-cache'
));

$client->enqueue($request)->send();
$response = $client->getResponse();

echo $response->getBody();

我希望你能帮助我

0 个答案:

没有答案