Laravel. Redirect intended to post method
Solution 1
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
Solution 2
You have to trick Laravel router by passing an "_method" the inputs.
The best way I found is by adding tricking and rewriting the Authenticate middleware
You have to rewrite the handle method to allow your redirection with your new input.
redirect()->guest('your/path')->with('_method', session('url.entended.method', 'GET'));
When you want to redirect to a route using another method than GET, simply do a Session::flash('url.entended.method', 'YOUR_METHOD')
.
Tell me if it do the trick
Solution 3
change the below code in app\exceptions\handler.php
use Exception;
use Request;
use Illuminate\Auth\AuthenticationException;
use Response;
protected function unauthenticated($request,AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
//return redirect()->guest(route('login'));
return redirect()->guest('http://127.0.0.1:8000/api/signinnew'); // change this part to your login router
}
And in routes(i.e api.php):
Route::Any('signinnew', [UserLogonController::class, 'signinNew']);
This will work in laravel 8x
Solution 4
Very Simple Approach for Post method Route form Controller.
The idea behind this is, every Route always calls the Action method of a Controller. so that in that case you can directly call that method in place of Redirect action performed.
check a code sample of XYZController
$registration = Registration::find($req->regId);
$registration->update([ 'STEP_COMPLETED' => 5]); // Step 5 completed.
# Call Post Method Route
return $this->admissionFinish($req);
Note that $req should have all parameter that required in next action Method.
Evaldas Butkus
Updated on February 13, 2021Comments
-
Evaldas Butkus about 3 years
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.
-
Evaldas Butkus almost 9 yearsHmm, i underastand ideo behind this, but problem is that redirection to post method happens on successfull login. This is my redirection after loggin in: return redirect()->intended(); How do i define this redirect?
-
Emeka Mbah almost 9 yearsOk maybe its possible dough. this is what redirect method looks like in Laravel.
function redirect($to = null, $status = 302, $headers = array(), $secure = null) { if (is_null($to)) return app('redirect'); return app('redirect')->to($to, $status, $headers, $secure); }
lemme see if this can be done via $headers -
Emeka Mbah almost 9 years@Evaldus Butkus Please do you also with to send some form data along to
redirect()->guest('user/additional-fields');
-
Evaldas Butkus almost 9 yearsYes i do. return redirect()->guest('user/review/information/' .$code); So as i udnerstand I need to get to that view where post method happened.
-
Emeka Mbah almost 9 yearshmm. can you make the request flow clearer? I understand 1. You do this
return redirect()->guest('user/additional-fields');
2. From user/additional-fields controller method you nowreturn redirect()->intended();
please where doesreturn redirect()->guest('user/review/information/' .$code);
fits into this flow? -
Evaldas Butkus almost 9 yearsI will try to make it all clear. First(1): return redirect()->guest('user/review/information/' .$code); From there user can fill in additional information OR log in. If he loggs in (2): return redirect()->intended(); and than error happens, becouse application redirects me into POST method. Original post method route: Route::post('/review/new-post', ['as' => 'review-new-post', 'uses' => 'ReviewController@postReviewNew']);
-
Emeka Mbah almost 9 yearsThis should be simple then. Please let me see the code for you routes especially
user/review/information/
. Do you have a similar post route like thisuser/review/information/{code?}
that might be conflicting with get? or did you forget to include{code}
parameter? -
Evaldas Butkus almost 9 yearsPart with user/review/information/{code} works just perfectly. I will repeat all the process. I fill in form X fields, on buton submit create record (if not logged in) middleware catches user and redirects him(user/review/information/{code}) and suggests him to fill in additional fields OR log in with exiting accout. If user decides to log in he get redirected back (redirect()->intended();), back means into form X view but into POST method and than error appears.
-
Emeka Mbah almost 9 yearsthat's wired. its suppose to redirected you back to a get route. example,
user/review/information/123456
which return a view containing the form. please just to confirm just before returnredirect()->intended()
do dd(\Session::get('url.entended')); so as to see if the URL is correct -
Evaldas Butkus almost 9 yearsIt's becouse middleware starts working on POST method, so redirect intended redirect also to POST method. But laravel sees my POST method as GET
-
Emeka Mbah almost 9 yearsso issue is likely from your middleware? please also share the code for your middleware
-
Evaldas Butkus almost 9 yearsIssue is that applicatiuon redirects inteded into POST route, becouse middleware catches him there. Route::group(['middleware' => 'reviewAuth'], function() { Route::post('/review/new-post', ['as' => 'review-new-post', 'uses' => 'ReviewController@postReviewNew']); }); Route redirect()->intended() redirects user into /review/new-pos with error
-
Emeka Mbah almost 9 yearsLet us continue this discussion in chat.
-
pankaj kumar about 4 yearsreturn redirect()->guest('oams/admission-finish')->withInput([ 'sid'=>$req->sid,'regId'=>$req->regId]) ->with(['_method'=> session('url.entended.method', 'POST')]);