112

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.

spacemonkey
  • 2,530
  • 6
  • 23
  • 27

19 Answers19

203

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;
}
Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
hayhorse
  • 2,652
  • 1
  • 10
  • 7
27

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>
Blessing
  • 4,858
  • 1
  • 18
  • 13
23

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

Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
Eli
  • 231
  • 2
  • 3
13

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');
minitechi
  • 134
  • 1
  • 8
  • This is correct, can a moderator make this the correct answer if the user isn't going to? This is the 3rd answer down and it's the only one that's right. – logos_164 Jul 09 '19 at 14:58
12

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.

Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
Dan
  • 9,391
  • 5
  • 41
  • 73
10

Typically MethodNotAllowedHttpException happens when

route method does not match.

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

Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
Kousher Alam
  • 1,005
  • 1
  • 15
  • 30
7
<?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'))
Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
itachi
  • 6,323
  • 3
  • 30
  • 40
5

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.

Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
iSWORD
  • 768
  • 15
  • 22
4

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)) }}
Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
Faruk Omar
  • 1,173
  • 2
  • 14
  • 22
3

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

Calling PUT route with POST request

Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
Slimane MEHARZI
  • 334
  • 5
  • 11
2

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.

Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
philthathril
  • 476
  • 5
  • 8
2

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

Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
Ahmed Albarody
  • 167
  • 1
  • 9
0

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.

newUserName02
  • 1,598
  • 12
  • 17
Fillz Adebayo
  • 420
  • 3
  • 6
0

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

Ariful Islam
  • 696
  • 6
  • 11
0

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.

GabrielFiel
  • 67
  • 2
  • 7
0

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

0

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.

RazorHead
  • 1,460
  • 3
  • 14
  • 20
0

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

Alejo Florez
  • 101
  • 4
-1
// 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 !');
Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263