Skip to content
Advertisement

Laravel. Redirect intended to post method

I’m using laravel 5 and this is my problem. User fill in form X and if he isin’t logged in, he gets redirected to fill in more fields form OR he gets possibility to log in. Everything works just fine, if user fill in additional fields, but if he login, laravel redirects user to form X with GET method instead of POST.

This is how my middleware redirect looks like:

return redirect()->guest('user/additional-fields');

This redirect appears on successfull log in:

return redirect()->intended();

So on redirect intended i get error MethodNotAllowedHttpException. URL is correct which is defined as POST method. What am I missing here? Why does laravel redirects intended as GET method? How could I solve this problem? Thanks!

EDIT:

Route::post('/user/log-in-post', ['as' => 'user-log-in-post', 'uses' => 'UserController@postUserLogIn']);

This is my route, I hope this is one you need.

Advertisement

Answer

You can use a named route to solve this issue:

Lets make a named route like this:

For Get

Route::get('user/additional-fields',array(
    'uses' => 'UserController@getAdditionalFields',
    'as'   => 'user.getAdditionalFields'
));

For post

Route::post('user/additional-fields',array(
    'uses' => 'UserController@postAdditionalFields',
    'as'   => 'user.postAdditionalFields'
));

So we can now ensure Laravel uses the right route by doing this

return redirect()->guest(route('user.getAdditionalFields'));

Also note that its not possible to redirect a POST because Laravel expects form to be submitted. SO you can’t do this:

return redirect()->guest(route('user.postAdditionalFields'));

except you use something like cURL or GuzzleHttp simulate a post request

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement