Laravel. Redirection for method dispatch

I am using laravel 5 and this is my problem. The user fills out Form X and if he is not logged in, he is redirected to fill in more fields, or he has the option to log in. Everything works fine if the user fills in additional fields, but if he is logged in, the laravel redirects the user has to form an X using the GET method instead of POST.

This is what my middleware redirect looks like:

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

      

This redirect appears on successful login:

return redirect()->intended();

      

So in the redirect, I want to get a MethodNotAllowedHttpException error. Correct url which is defined as POST method. What am I missing here? Why are laravel relays meant to be a GET method? How can I solve this problem? Thank!

EDIT:

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

      

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

+3


source to share


2 answers


You can use a named route to solve this problem:

Lets make a named route like this:

To receive

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

      

For messages

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

      



So now we can ensure that Laravel is using the correct route by doing this

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

Also note that it cannot be redirected POST

because Laravel expects the form to be submitted. This is how you cannot do it:

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

except you are using something like cURL or GuzzleHttp, simulate a post request

+4


source


You have to trick the Laravel router by passing "_method" inputs.

The best way I have found is to add a trick and rewrite the Authenticate middleware

You must rewrite the descriptor method to allow new entry redirection.



redirect()->guest('your/path')->with('_method', session('url.entended.method', 'GET'));

      

If you want to redirect the route using a different method than GET, just do Session::flash('url.entended.method', 'YOUR_METHOD')

.

Tell me if this is a trick

+2


source







All Articles