Sjoerd de Wit
Sjoerd de Wit

Reputation: 2413

Laravel 5.2 PUT request gives MethodNotAllowedHttpException

So I'm having troubles with a PUT request in laravel.

I've done my research and know that you should simulate a PUT request with _method = PUT and change it to POST because HTML doesn't support PATCH and PUT.

However postman is still returning the MethodNotAllowedHttpException, I've also added the url to the $except array in the VerifyCsrfToken class. I've also tried adding it as a GET variable to the end of the url with no success.

The error log returned by 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

These are my routes:

+--------+----------+-----------------------------------+------+-----------------------------------------------------------------+------------+
| 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   |
+--------+----------+-----------------------------------+------+-----------------------------------------------------------------+------------+

Here are my routes in script:

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


});

postman code:

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

So does anyone have an idea what could be going wrong?

Upvotes: 3

Views: 797

Answers (1)

Sjoerd de Wit
Sjoerd de Wit

Reputation: 2413

It was a problem with my .htaccess, the server didn't allow put and delete request. added these lines to make it work:

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

Hope this helps someone in the future

Upvotes: 2

Related Questions