Redirect to Previous Page or URL after Login Laravel

Laravel 6 , Laravel , Laravel 7

Nicesnippets

1973

10-07-2020


Redirect to Previous Page or URL after Login Laravel

Hi Guys,

In this blog, I would like to share you how to redirect back to previous page or url after login in laravel application. I will show you laravel redirect to previous page after login example.

In this tutorial, I will add two method into login controller. We will redirect to previous url after login in laravel app.

Method 1 : Show Login Form

In this method, You can add this method into logincontroller to show login form.So Let's open LoginController and add this method.This function will set the “url.intended” session variable. This variable to look for the page in which the user will be redirected after login.

app/Http/Controllers/Auth/LoginController.php

public function showLoginForm()

{

if(!session()->has('url.intended'))

{

session(['url.intended' => url()->previous()]);

}

return view('auth.login');

}

Method 2 : Show Login Form

In this method, We have to add __contruct() method in redirect previous url or page inside LoginController.

$this->redirectTo = url()->previous();

app/Http/Controllers/Auth/LoginController.php

public function __construct()

{

$this->middleware('guest')->except('logout');

$this->redirectTo = url()->previous();

}

It will help you...