Home >Backend Development >PHP Tutorial >How to Redirect Users to Their Original Destination After Laravel Login?
Laravel: Redirect User to Original Destination After Login
In Laravel, when a user attempts to access a protected route, they are redirected to the login page. After logging in, the user should be redirected back to the page they were trying to access originally.
Solution
For Laravel 5.3 and Above
// Login action public function login(Request $request) { if (Auth::attempt($request->all())) { return redirect()->intended('dashboard'); } return redirect('login'); }
For Laravel 5 Up to 5.2
// Auth middleware public function handle($request, Closure $next) { if (Auth::guest()) { Session::put('intended_url', request()->url()); return redirect('login'); } return $next($request); } // Login action public function login(Request $request) { if (Auth::attempt($request->all())) { return redirect()->intended('dashboard'); } return redirect('login'); }
For Laravel 4
// Auth filter Route::filter('auth', function($route, $request) { if (Auth::guest()) { Session::put('intended_url', request()->url()); return Redirect::guest('login'); } }); // Login action public function login(Request $request) { if (Auth::attempt($request->all())) { return Redirect::intended('dashboard'); } return Redirect::to('login'); }
The above is the detailed content of How to Redirect Users to Their Original Destination After Laravel Login?. For more information, please follow other related articles on the PHP Chinese website!