CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   Laravel   How to Redirect Back to Previous Page or URL After Logging i ...

How to Redirect Back to Previous Page or URL After Logging in Laravel ?

September 9, 2018 by SNK

laravel redirect back to previous page after login

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 ?

  1. Build the redirecting back to previous page using login.php.
  2. 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.

Login.blade.php
PHP
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.

LoginController.php
PHP
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,

Default Login Code
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.

SHARE ON
Buffer

Enjoyed this article?

Like us on

Laravel laravel redirect to previous page after login redirecting to previous url in laravel

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.