We can override this property to redirect users after login in LoginController:
protected $redirectTo = '/home';
And here is the statement from documentation:
If the redirect path needs custom generation logic you may define a redirectTo method instead of a redirectTo property:
protected function redirectTo() {
// }
But it always redirects to '/home';
whatever the condition is.
protected function redirectTo()
{
if (Auth::user()->role==0) {
return '/volunteer';
} else {
return '/donor';
}
}
If a method exists it'll use it, otherwise the property will be used. But it looks like property is being used even if the method exists.
However overriding authenticated()
or sendLoginResponse()
function works fine.
protected function authenticated()
{
if (Auth::user()->role==0) {
return redirect('/volunteer') ;
} else {
return redirect('/donor');
}
}
What is wrong with redirectTo()
method there? Here is the GitHub source code to these methods.
I'm using Laravel version 5.3.28.
redirectPath()
instead ofredirectTo()
– TroyerredirectTo()
method? – Sanzeeb Aryal$this->redirectTo = '/volunteer'
nor overridingredirectPath()
works. but, it is fine with overridingauthenticated()
method with same code. – Sanzeeb Aryal