Laravel 5.2 PUT请求给出了MethodNotAllowedHttpException

时间:2016-04-20 14:53:35

标签: php laravel laravel-routing put

所以我在laravel中遇到PUT请求时遇到了麻烦。

我已经完成了我的研究并知道您应该使用PUT = _method模拟PUT请求并将其更改为POST,因为HTML不支持{ {1}}和PATCH

然而postman仍然返回MethodNotAllowedHttpException,我还将url添加到VerifyCsrfToken类中的PUT数组中。我也尝试将其作为$except变量添加到网址的末尾,但没有成功。

laravel返回的错误日志:

GET

这些是我的路线:

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');
    });


});

所以有人知道会出现什么问题吗?

1 个答案:

答案 0 :(得分:2)

我的.htaccess存在问题,服务器不允许放置和删除请求。添加这些行以使其工作:

<Limit GET POST PUT DELETE>
  Allow from all
</Limit>

希望这可以帮助将来的某个人