0

I have to access to PUT route via browser and get json object, but L5.2 show me an MethodNotAllowedHttpException error what have I do to for fix this ?

My URL http://laravel5.restapi.dev/a?_method=put

My route:list

route.php

<?php

Route::put('/a', function () {
   return view('welcome');
});

Output

Whoops, looks like something went wrong.

MethodNotAllowedHttpException in RouteCollection.php line 218:
in RouteCollection.php line 218
at RouteCollection->methodNotAllowed(array('PUT')) in RouteCollection.php line 205
at RouteCollection->getRouteForMethods(object(Request), array('PUT')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 821
at Router->findRoute(object(Request)) in Router.php line 691
at Router->dispatchToRoute(object(Request)) in Router.php line 675
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 Debugbar.php line 49
at Debugbar->handle(object(Request), object(Closure))
at call_user_func_array(array(object(Debugbar), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
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)) 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 136
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 102
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 53
  • 2
    Your route has a `PSOT` declaration. You say you're using `PUT`, that's enough to understand a "Method Not Allowed". But, when you say `accessing via browser`, what do you mean? Direct URL access via browsers can only make a `GET`. – Marco Aurélio Deleu Aug 19 '16 at 12:03
  • wait....you want `PUT`, you define `POST` in code, and expect it to handle `PUT`? I don't understand. – Pete Houston Aug 19 '16 at 13:43
  • @PeteHouston Sorry for error I define Route::put not Route::post – Samvel Budumyan Aug 19 '16 at 15:17

3 Answers3

1

PUT method works only when your Form Method is POST and then you add _method input field with value PUT.

<form method="post">
    <input type="hidden" name="_method" value="PUT">
</form>
jaysingkar
  • 4,315
  • 1
  • 18
  • 26
  • It also works if your form method is PUT, in browsers that support it. – ceejayoz Aug 19 '16 at 13:51
  • @ceejayoz yes .. but It was only supported in Mozilla firefox. And as given in http://stackoverflow.com/questions/8054165/using-put-method-in-html-form "XHTML 1.x forms only support GET and POST. GET and POST are the only allowed values for the "method" attribute." – jaysingkar Aug 19 '16 at 14:04
  • @jaysingkar Is there any recipe to do same but with Form Method GET ? – Samvel Budumyan Aug 19 '16 at 15:26
  • Yes.. but its not a standard method... what you can do is get the `_method` parameter in your using `$_GET['_method']` and check if its PUT – jaysingkar Aug 19 '16 at 16:24
  • could you tell me the reason for using get ? as, it it not recommended – jaysingkar Aug 19 '16 at 16:24
  • @jaysingkar I finally find what I need. I want have method spoofing not only in POST request but in GET either . Damn ( – Samvel Budumyan Aug 19 '16 at 16:53
  • @SamvelBudumyan GET requests should never be destructive. PUT/PATCH/DELETE should have to use POST if you're using method spoofing. – ceejayoz Aug 20 '16 at 02:59
1

The PUT-request is not allowed since you're using Route::post($uri, $callback).

You need to specify your route as Route::put($uri, $callback) or (if you need to match other requests, too):

Route::match(['post', 'put'], '/a', function () {
    // for post & put requests
});

Route::any('/a', function () {
    // for all methods
});

As said in the comments and in the other answer, PUT-requests must be POST-requests with the additional parameter _method=PUT.

Please refer to: https://laravel.com/docs/5.2/routing#basic-routing

sleepless
  • 1,769
  • 1
  • 22
  • 33
0

you can also use

    {{ method_field('PUT') }}

inside your form with method = post . so that method_field converts it to put

Then in your routes.php

     Route::put(.....);

or

    Route::any(...);
Ahmed Nawaz Khan
  • 1,451
  • 3
  • 20
  • 42