Laravel. Redirect intended to post method

4

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

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

منبع : کلیک کنید

سیدعباس حسینی

نویسنده: AbbasHoseini

به دنبال خلق رویام تو دنیای برنامه نویسی پیرو دنیای آزاد

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *