[routing] laravel throwing MethodNotAllowedHttpException

I am trying to get something very basic running. I am used to CI and now learning Laravel 4, and their docs are not making it easy! Anyways, I am trying to create a login form and just make sure that data is posted successfully by printing it in the next form. I am getting this exception:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

and my MemberController.php:

    public function index()
    {
        if (Session::has('userToken'))
        {
            /*Retrieve data of user from DB using token & Load view*/
            return View::make('members/profile');
        }else{
            return View::make('members/login');
        }
    }

    public function validateCredentials()
    {
        if(Input::post())
        {
            $email = Input::post('email');
            $password = Input::post('password');
            return "Email: " . $email . " and Password: " . $password;
        }else{
            return View::make('members/login');
        }
    }

and routes has:

Route::get('/', function()
{
    return View::make('hello');
});

Route::get('/members', 'MemberController@index');
Route::get('/validate', 'MemberController@validateCredentials');

and finally my view login.php has this form direction:

<?php echo Form::open(array('action' => 'MemberController@validateCredentials')); ?>

Any help will be greatly appreciated.

This question is related to routing laravel laravel-4

The answer is


In my case, it was because my form was sending to a route with a different middleware. So it blocked from sending information to this specific route.


well when i had these problem i faced 2 code errors

{!! Form::model(['method' => 'POST','route' => ['message.store']]) !!}

i corrected it by doing this

{!! Form::open(['method' => 'POST','route' => 'message.store']) !!}

so just to expatiate i changed the form model to open and also the route where wrongly placed in square braces.


I also had the same error but had a different fix, in my XYZ.blade.php I had:

{!! Form::open(array('ul' => 'services.store')) !!}

which gave me the error, - I still don't know why- but when I changed it to

{!! Form::open(array('route' => 'services.store')) !!}

It worked!

I thought it was worth sharing :)


That is because you are posting data through a get method.

Instead of

Route::get('/validate', 'MemberController@validateCredentials');

Try this

Route::post('/validate', 'MemberController@validateCredentials');

<?php echo Form::open(array('action' => 'MemberController@validateCredentials')); ?>

by default, Form::open() assumes a POST method.

you have GET in your routes. change it to POST in the corresponding route.

or if you want to use the GET method, then add the method param.

e.g.

Form::open(array('url' => 'foo/bar', 'method' => 'get'))

My problem was not that my routes were set up incorrectly, but that I was referencing the wrong Form method (which I had copied from a different form). I was doing...

{!! Form::model([ ... ]) !!}

(with no model specified). But I should have been using the regular open method...

{!! Form::open([ ... ]) !!}

Because the first parameter to model expect an actual model, it was not getting any of my options I was specifying. Hope this helps someone who knows their routes are correct, but something else is amiss.


My suspicion is the problem lies in your route definition.

You defined the route as a GET request but the form is probably sending a POST request. Change your route definition to match the form's request method.

Route::post('/validate', [MemberController::class, 'validateCredentials']);

It's generally better practice to use named routes (helps to scale if the controller method/class changes).

Route::post('/validate', [MemberController::class, 'validateCredentials'])
    ->name('member.validateCredentials');

In the view, use the validation route as the form's action.

<form action="{{ route('member.validateCredentials') }}" method="POST">
  @csrf
...
</form>

Laravel sometimes does not support {!! Form::open(['url' => 'posts/store']) !!} for security reasons. That's why the error has happened. You can solve this error by simply replacing the below code

{!! Form::open(array('route' => 'posts.store')) !!}




Error Code {!! Form::open(['url' => 'posts/store']) !!}

Correct Code {!! Form::open(array('route' => 'posts.store')) !!}


I faced the error,
problem was FORM METHOD

{{ Form::open(array('url' => 'admin/doctor/edit/'.$doctor->doctor_id,'class'=>'form-horizontal form-bordered form-row-stripped','method' => 'PUT','files'=>true)) }}

It should be like this

{{ Form::open(array('url' => 'admin/doctor/edit/'.$doctor->doctor_id,'class'=>'form-horizontal form-bordered form-row-stripped','method' => 'POST','files'=>true)) }}

In my case, I was sending a POST request over HTTP to a server where I had set up Nginx to redirect all requests to port 80 to port 443 where I was serving the app over HTTPS.

Making the request to the correct port directly fixed the problem. In my case, all I had to do is replace http:// in the request URL to https:// since I was using the default ports 80 and 443 respectively.


I encountered this problem as well and the other answers here were helpful, but I am using a Route::resource which takes care of GET, POST, and other requests.

In my case I left my route as is:

Route::resource('file', 'FilesController');

And simply modified my form to submit to the store function in my FilesController

{{ Form::open(array('route' => 'file.store')) }}

This fixed the issue, and I thought it was worth pointing out as a separate answer since various other answers suggest adding a new POST route. This is an option but it's not necessary.


Typically MethodNotAllowedHttpException happens when

route method does not match.

Suppose you define POST request route file, but you sending GET request to the route.


The problem is the you are using POST but actually you have to perform PATCH To fix this add

<input name="_method" type="hidden" value="PATCH">

Just after the Form::model line


You are getting that error because you are posting to a GET route.

I would split your routing for validate into a separate GET and POST routes.

New Routes:

Route::post('validate', 'MemberController@validateCredentials');

Route::get('validate', function () {
    return View::make('members/login');
});

Then your controller method could just be

public function validateCredentials()
{
    $email = Input::post('email');
    $password = Input::post('password');
    return "Email: " . $email . " and Password: " . $password;
}

// not done
Route::post('`/posts/{id}`', 'PostsController@store')->name('posts.store');

return redirect('`/posts'`)->with('status','Post was created !');

// done
Route::post('`/posts`', 'PostsController@store')->name('posts.store');

return redirect('`/posts'`)->with('status','Post was created !');

Generally, there is a mistake in the HTTP verb used eg:

Calling PUT route with POST request


Examples related to routing

Send data through routing paths in Angular Angular2 Routing with Hashtag to page anchor Passive Link in Angular 2 - <a href=""> equivalent laravel throwing MethodNotAllowedHttpException How to use absolute path in twig functions S3 Static Website Hosting Route All Paths to Index.html AngularJS - How can I do a redirect with a full page load? Web API Routing - api/{controller}/{action}/{id} "dysfunctions" api/{controller}/{id} The requested resource does not support HTTP method 'GET' My Routes are Returning a 404, How can I Fix Them?

Examples related to laravel

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration Target class controller does not exist - Laravel 8 Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required How can I run specific migration in laravel Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory

Examples related to laravel-4

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration 'Malformed UTF-8 characters, possibly incorrectly encoded' in Laravel Can I do Model->where('id', ARRAY) multiple where conditions? how to fix stream_socket_enable_crypto(): SSL operation failed with code 1 Rollback one specific migration in Laravel How can I resolve "Your requirements could not be resolved to an installable set of packages" error? Define the selected option with the old input in Laravel / Blade Redirect to external URL with return in laravel laravel the requested url was not found on this server