Laravel 5.2 PUT 请求给出 MethodNotAllowedHttpException
Laravel 5.2 PUT request gives MethodNotAllowedHttpException
所以我在 laravel 中遇到了 PUT
请求的问题。
我做了研究,知道你应该用 _method
= PUT
模拟一个 PUT
请求并将其更改为 POST
因为 HTML 不支持 PATCH
和 PUT
。
但是邮递员仍然返回 MethodNotAllowedHttpException,我还在 VerifyCsrfToken class 中的 $except
数组中添加了 url。我还尝试将它作为 GET
变量添加到 url 的末尾,但没有成功。
laravel返回的错误日志:
in RouteCollection.php line 219
at RouteCollection->methodNotAllowed(array('PUT')) in RouteCollection.php line 206
at RouteCollection->getRouteForMethods(object(Request), array('PUT')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 802
at Router->findRoute(object(Request)) in Router.php line 670
at Router->dispatchToRoute(object(Request)) in Router.php line 654
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
这些是我的路线:
+--------+----------+-----------------------------------+------+-----------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-----------------------------------+------+-----------------------------------------------------------------+------------+
| | GET|HEAD | / | | Closure | web |
| | PUT | api/user | | App\Http\Controllers\UsersController@createUser | |
| | POST | dashboard/admin/updatedate | | App\Http\Controllers\AdminsController@updateDate | web,auth |
| | GET|HEAD | dashboard/alerts | | App\Http\Controllers\AlertsController@index | web,auth |
| | GET|HEAD | dashboard/categorie/{id} | | App\Http\Controllers\CategorieController@index | web,auth |
| | GET|HEAD | dashboard/development | | App\Http\Controllers\DevelopmentController@index | web,auth |
| | GET|HEAD | dashboard/home | | App\Http\Controllers\HomeController@index | web,auth |
| | GET|HEAD | dashboard/login | | App\Http\Controllers\Auth\AuthController@showLoginForm | web,guest |
| | POST | dashboard/login | | App\Http\Controllers\Auth\AuthController@login | web,guest |
| | GET|HEAD | dashboard/logout | | App\Http\Controllers\Auth\AuthController@logout | web |
| | POST | dashboard/password/email | | App\Http\Controllers\Auth\PasswordController@sendResetLinkEmail | web,guest |
| | POST | dashboard/password/reset | | App\Http\Controllers\Auth\PasswordController@reset | web,guest |
| | GET|HEAD | dashboard/password/reset/{token?} | | App\Http\Controllers\Auth\PasswordController@showResetForm | web,guest |
| | POST | dashboard/register | | App\Http\Controllers\Auth\AuthController@register | web,guest |
| | GET|HEAD | dashboard/register | | App\Http\Controllers\Auth\AuthController@showRegistrationForm | web,guest |
| | GET|HEAD | dashboard/zone/{id} | | App\Http\Controllers\ZoneController@index | web,auth |
+--------+----------+-----------------------------------+------+-----------------------------------------------------------------+------------+
这是我在脚本中的路线:
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('pvn.index');
});
Route::group(['prefix' => 'api'], function () {
Route::put('/user', 'UsersController@createUser');
});
Route::group(['prefix' => 'app'], function () {
return view('app.map.view');
});
Route::group(['prefix' => 'dashboard'], function () {
Route::auth();
});
});
Route::group(['middleware' => ['web','auth']], function () {
Route::group(['prefix' => 'dashboard'], function () {
Route::post('/admin/updatedate', 'AdminsController@updateDate');
Route::get('/home', 'HomeController@index');
Route::get('/development', 'DevelopmentController@index');
Route::get('/categorie/{id}', 'CategorieController@index');
Route::get('/zone/{id}', 'ZoneController@index');
Route::get('/alerts', 'AlertsController@index');
});
});
邮递员代码:
PUT /api/user HTTP/1.1
Host: platformveilignederland.nl
Cache-Control: no-cache
Postman-Token: 8dd46b47-ad06-151e-57de-7ddf37ee4f66
Content-Type: application/x-www-form-urlencoded
user=gertje
所以有没有人知道可能出了什么问题?
我的 .htaccess 有问题,服务器不允许放置和删除请求。添加这些行以使其工作:
<Limit GET POST PUT DELETE>
Allow from all
</Limit>
希望这对以后的人有所帮助
所以我在 laravel 中遇到了 PUT
请求的问题。
我做了研究,知道你应该用 _method
= PUT
模拟一个 PUT
请求并将其更改为 POST
因为 HTML 不支持 PATCH
和 PUT
。
但是邮递员仍然返回 MethodNotAllowedHttpException,我还在 VerifyCsrfToken class 中的 $except
数组中添加了 url。我还尝试将它作为 GET
变量添加到 url 的末尾,但没有成功。
laravel返回的错误日志:
in RouteCollection.php line 219
at RouteCollection->methodNotAllowed(array('PUT')) in RouteCollection.php line 206
at RouteCollection->getRouteForMethods(object(Request), array('PUT')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 802
at Router->findRoute(object(Request)) in Router.php line 670
at Router->dispatchToRoute(object(Request)) in Router.php line 654
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
这些是我的路线:
+--------+----------+-----------------------------------+------+-----------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-----------------------------------+------+-----------------------------------------------------------------+------------+
| | GET|HEAD | / | | Closure | web |
| | PUT | api/user | | App\Http\Controllers\UsersController@createUser | |
| | POST | dashboard/admin/updatedate | | App\Http\Controllers\AdminsController@updateDate | web,auth |
| | GET|HEAD | dashboard/alerts | | App\Http\Controllers\AlertsController@index | web,auth |
| | GET|HEAD | dashboard/categorie/{id} | | App\Http\Controllers\CategorieController@index | web,auth |
| | GET|HEAD | dashboard/development | | App\Http\Controllers\DevelopmentController@index | web,auth |
| | GET|HEAD | dashboard/home | | App\Http\Controllers\HomeController@index | web,auth |
| | GET|HEAD | dashboard/login | | App\Http\Controllers\Auth\AuthController@showLoginForm | web,guest |
| | POST | dashboard/login | | App\Http\Controllers\Auth\AuthController@login | web,guest |
| | GET|HEAD | dashboard/logout | | App\Http\Controllers\Auth\AuthController@logout | web |
| | POST | dashboard/password/email | | App\Http\Controllers\Auth\PasswordController@sendResetLinkEmail | web,guest |
| | POST | dashboard/password/reset | | App\Http\Controllers\Auth\PasswordController@reset | web,guest |
| | GET|HEAD | dashboard/password/reset/{token?} | | App\Http\Controllers\Auth\PasswordController@showResetForm | web,guest |
| | POST | dashboard/register | | App\Http\Controllers\Auth\AuthController@register | web,guest |
| | GET|HEAD | dashboard/register | | App\Http\Controllers\Auth\AuthController@showRegistrationForm | web,guest |
| | GET|HEAD | dashboard/zone/{id} | | App\Http\Controllers\ZoneController@index | web,auth |
+--------+----------+-----------------------------------+------+-----------------------------------------------------------------+------------+
这是我在脚本中的路线:
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('pvn.index');
});
Route::group(['prefix' => 'api'], function () {
Route::put('/user', 'UsersController@createUser');
});
Route::group(['prefix' => 'app'], function () {
return view('app.map.view');
});
Route::group(['prefix' => 'dashboard'], function () {
Route::auth();
});
});
Route::group(['middleware' => ['web','auth']], function () {
Route::group(['prefix' => 'dashboard'], function () {
Route::post('/admin/updatedate', 'AdminsController@updateDate');
Route::get('/home', 'HomeController@index');
Route::get('/development', 'DevelopmentController@index');
Route::get('/categorie/{id}', 'CategorieController@index');
Route::get('/zone/{id}', 'ZoneController@index');
Route::get('/alerts', 'AlertsController@index');
});
});
邮递员代码:
PUT /api/user HTTP/1.1
Host: platformveilignederland.nl
Cache-Control: no-cache
Postman-Token: 8dd46b47-ad06-151e-57de-7ddf37ee4f66
Content-Type: application/x-www-form-urlencoded
user=gertje
所以有没有人知道可能出了什么问题?
我的 .htaccess 有问题,服务器不允许放置和删除请求。添加这些行以使其工作:
<Limit GET POST PUT DELETE>
Allow from all
</Limit>
希望这对以后的人有所帮助