Redirect to Previous Page or URL after Login Laravel
Jul 10, 2020
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...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial