Home >Backend Development >PHP Tutorial >How to Redirect Users to Their Original Destination After Login in Laravel?
Upon user authentication, it's required to redirect to the original page that prompted the login requirement. However, determining this original destination can be challenging.
For Laravel 5.3 and Above
Refer to Scott's answer provided below.
For Laravel 5 Up to 5.2
Auth Middleware:
// redirect the user to "/login" // and stores the url being accessed on session if (Auth::guest()) { return redirect()->guest('login'); } return $next($request);
Login Action:
// redirect the user back to the intended page // or defaultpage if there isn't one if (Auth::attempt(['email' => $email, 'password' => $password])) { return redirect()->intended('defaultpage'); }
For Laravel 5.3 and Above
// auth middleware Auth::routes(); // generates route for all authentication // redirect to original page after auth Redirect::intended('/profile');
The above is the detailed content of How to Redirect Users to Their Original Destination After Login in Laravel?. For more information, please follow other related articles on the PHP Chinese website!