• Register
0 votes
57 views

Problem :

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

“Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException”

 Any help in resolving above error will be greatly appreciated.

3.9k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

You are facing that error as you are posting to the GET route.

I want to split your routing for validate into the separate GET and POST routes as shown below :

The New Routes:

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

Then your controller method could just be as shown below

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

 OR

Typically the MethodNotAllowedHttpException happens only when the

route method does not match.

Suppose you have defined POST request route file, however you are sending GET request to a route.

I know above given solutions will help you in resolving all the errors.

38.6k points
0 votes

Solution:

You are obtaining that error since you are posting to a GET route.

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

Thereafter your controller method could only be

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

My doubt is the problem lies in your route definition.

You identified the route as a GET request however the form is possibly sending a POST request. Alter your route definition.

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

It's usually better practice to use named routes (uses to scale in case the controller method/class changes).

Route::post('/validate', array(
    'as' => 'validate',
    'uses' => 'MemberController@validateCredentials'
));

In the form employ the following

<?php echo Form::open(array('route' => 'validate')); ?>

The issue is the you are employing POST however really you have to perform PATCH To solve this add

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

Only after the Form::model line

I encountered this issue as well and the other answers here were useful, however I am using a Route::resource which takes care of GETPOST, and other requests.

In my instance I left my route as is:

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

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

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

This solved the issue, and I thought it was worth pointing out as a separate answer whereas several other answers point out including a new POST route. This is an option however it's not required.

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

Instead of

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

Attempt this

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

 

31.1k points
edited by