laravel throwing MethodNotAllowedHttpException

RoutingLaravelLaravel 4

Routing Problem Overview


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.

Routing Solutions


Solution 1 - Routing

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

Solution 2 - Routing

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>

Solution 3 - Routing

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

Solution 4 - Routing

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

Solution 5 - Routing

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.

Solution 6 - Routing

Typically MethodNotAllowedHttpException happens when

> route method does not match.

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

Solution 7 - Routing

<?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'))

Solution 8 - Routing

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.

Solution 9 - Routing

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

Solution 10 - Routing

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

Calling PUT route with POST request

Solution 11 - Routing

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.

Solution 12 - Routing

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 :)

Solution 13 - Routing

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.

Solution 14 - Routing

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

Solution 15 - Routing

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.

Solution 16 - Routing

As answered here by rebduvid you can use Route::match as below

Route::match(['get', 'post'], 'results',[
        'as' => 'results_path',
        'uses' => 'SearchController@results' ]);

update the parameters as per your logic

Solution 17 - Routing

In my case, I was hitting an auth protected endpoint and my auth token was invalid. Getting a new token resolved the issue. This is a new older project I'm working on so there could be something in the implementation causing this.

Solution 18 - Routing

In my case, it worked to check the https protocol, I in postman had defined a route with http, when the server was with https. I just corrected it and it worked for me

Solution 19 - Routing

// 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 !');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionspacemonkeyView Question on Stackoverflow
Solution 1 - RoutinghayhorseView Answer on Stackoverflow
Solution 2 - RoutingBlessingView Answer on Stackoverflow
Solution 3 - RoutingEliView Answer on Stackoverflow
Solution 4 - RoutingminitechiView Answer on Stackoverflow
Solution 5 - RoutingDanView Answer on Stackoverflow
Solution 6 - RoutingKousher AlamView Answer on Stackoverflow
Solution 7 - RoutingitachiView Answer on Stackoverflow
Solution 8 - RoutingiSWORDView Answer on Stackoverflow
Solution 9 - RoutingFaruk OmarView Answer on Stackoverflow
Solution 10 - RoutingSlimane MEHARZIView Answer on Stackoverflow
Solution 11 - RoutingphilthathrilView Answer on Stackoverflow
Solution 12 - RoutingAhmed AlbarodyView Answer on Stackoverflow
Solution 13 - RoutingFillz AdebayoView Answer on Stackoverflow
Solution 14 - RoutingAriful IslamView Answer on Stackoverflow
Solution 15 - RoutingGabrielFielView Answer on Stackoverflow
Solution 16 - Routingstackoverflow accountView Answer on Stackoverflow
Solution 17 - RoutingRazorHeadView Answer on Stackoverflow
Solution 18 - RoutingAlejo FlorezView Answer on Stackoverflow
Solution 19 - RoutingAbdelhakim EzzahraouiView Answer on Stackoverflow