Home > Backend Development > PHP Tutorial > How to Redirect Users Back to the Previous Page After Laravel Authentication?

How to Redirect Users Back to the Previous Page After Laravel Authentication?

Susan Sarandon
Release: 2024-11-30 18:05:13
Original
450 people have browsed it

How to Redirect Users Back to the Previous Page After Laravel Authentication?

Laravel: Redirecting to Previous Page After Login

Question:

How can I redirect a user back to the page they were trying to access before encountering a Laravel authentication filter?

Answer:

For Laravel 5.3 and above:

Redirect to the intended page using request()->intended(), e.g.:

// Auth middleware
if (!Auth::check()) {
    return redirect()->intended('login');
}

// Login action
if (Auth::attempt(['email' => $email, 'password' => $password])) {
    return redirect()->intended('/');
}
Copy after login

For Laravel 5 up to 5.2:

  1. In the auth middleware, redirect to "login" and store the original URL in the session:
// Auth middleware
if (!Auth::check()) {
    return redirect()->guest('login');
}
Copy after login
  1. In the login action, redirect to the intended page or a default page:
// Login action
if (Auth::attempt(['email' => $email, 'password' => $password])) {
    return redirect()->intended('defaultpage');
}
Copy after login

For Laravel 4:

  1. In the auth filter, redirect to "login" and store the original URL in the session:
// Auth filter
Route::filter('auth', function() {
    if (!Auth::check()) {
        return Redirect::guest('login');
    }
});
Copy after login
  1. In the login action, redirect to the intended page or a default page:
// Login action
if (Auth::attempt(['email' => $email, 'password' => $password])) {
    return Redirect::intended('defaultpage');
}
Copy after login

For Laravel 3:

  1. In the auth filter, store the current URL in the session and redirect to the login page:
// Auth filter
Route::filter('auth', function() {
    if (!Auth::check()) {
        Session::put('redirect', URL::full());
        return Redirect::to('/login');
    }
});
Copy after login
  1. In the login action, redirect to the intended page or a default page:
// Login controller
public function get_login() {
    // ...
}

public function post_login() {
    // ...

    if (Auth::attempt($credentials)) {
        $redirect = Session::get('redirect');
        Session::forget('redirect');

        if ($redirect) {
            return Redirect::to($redirect);
        } else {
            return Redirect::to('defaultpage');
        }
    }

    // ...
}
Copy after login

The above is the detailed content of How to Redirect Users Back to the Previous Page After Laravel Authentication?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template