In this article we are going to learn how to redirect back to previous page after logging in laravel. If you notice that most of the eCommerce website supports this feature to build interactive and engaging customer experience. Let’s demonstrate with the simple example.
In an eCommerce website when the users visits the page. He / she simply selects the product and add them in the wish list or their cart. They can do this without login in the website. After selecting the certain product they move into the checkout, if they are logged in then, they are redirected to the checkout page.
Else, they are redirected to the login or signup page. This is where this tutorial posts comes into play. In the past we need to set the custom redirect, grab the URL and redirect user to previous URL.
But now, laravel supports redirecting back to the same page after logging in inside the App. This helps customer to get engaged in the same page and finally redirect back to the original page. It helps customer to remain intact in the page and proceed to checkout. This helps a website owner to build engagement for the end user.
lets achieve this feature step by step.
Let’s get started
What are we going to do ?
- Build the redirecting back to previous page using
login.php
. - Grabbing the previous URL from the login page inside the
LoginController.php
and Redirect.
Step 1 : Building the redirecting back to Previous Page Using Login Page
We need to make some tweaks in order to get the previous URL and redirect back to the page where the visitor comes from after logging in. So we will make some changes in the login.blade.php
Check out the code below. I have highlighted with the help of comment code inside it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">{{ __('Login') }}</div> <div class="card-body"> <form method="POST" action="{{ route('login') }}"> @csrf <div class="form-group row"> <label for="email" class="col-sm-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label> <div class="col-md-6"> <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required autofocus> @if ($errors->has('email')) <span class="invalid-feedback" role="alert"> <strong>{{ $errors->first('email') }}</strong> </span> @endif </div> </div> <!-- ============================= Fetching the Previous URL ================================= --> <div class="form-group"> @if (Request::has('previous')) <input type="hidden" name="previous_url" value="{{ Request::get('previous_url') }}"> @else <input type="hidden" name="previous_url" value="{{ URL::previous() }}"> @endif </div> <!-- ============================= Block Ends ================================= --> <div class="form-group row"> <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label> <div class="col-md-6"> <input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required> @if ($errors->has('password')) <span class="invalid-feedback" role="alert"> <strong>{{ $errors->first('password') }}</strong> </span> @endif </div> </div> <div class="form-group row"> <div class="col-md-6 offset-md-4"> <div class="form-check"> <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}> <label class="form-check-label" for="remember"> {{ __('Remember Me') }} </label> </div> </div> </div> <div class="form-group row mb-0"> <div class="col-md-8 offset-md-4"> <button type="submit" class="btn btn-primary"> {{ __('Login') }} </button> <a class="btn btn-link" href="{{ route('password.request') }}"> {{ __('Forgot Your Password?') }} </a> </div> </div> </form> </div> </div> </div> </div> </div> @endsection |
Step 2 : Grabbing Previous URL Inside the LoginController.php
Once you have made changes inside the login.php
its time to override some of the default methods of the Authentication of laravel.
If you haven’t read everything inside the authentication of laravel, now its really give time to its default configuration and methods. Take a look at redirectTo()
, AuthenticatesUsers.php
, RedirectUsers.php
We cannot touch the core files of the framework that’s why we are overriding their methods inside LoginController.php
. Let’s override redirectTo();
method inside LoginController.php
and modify to our needs.
1 2 3 4 5 6 7 8 |
public function redirectTo() { if ($this->request->has('previous_url')) { $this->redirectTo = $this->request->get('previous_url'); } return $this->redirectTo ?? '/home'; } |
One more thing that you need to take a note is that always pass parameters to the default login URL as below,
1 |
<a href="{{ route('login') . '?previous_url=' . Request::fullUrl() }}">Login</a> |
This is it, if you found this code useful, feel free to share and mention them in comments if have any problem.
Leave a Reply