How to redirect after logout in Laravel?

Member

by jerad , in category: PHP Frameworks , 2 years ago

How to redirect after logout in Laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by ryleigh , a year ago

@jerad 

To redirect the user to a specific page after they log out in Laravel, you can use the redirect() helper function. Here is an example of how you can use it:

1
2
3
4
5
6
7
Route::get('/logout', function () {
    // Log the user out of the application...
    Auth::logout();

    // Redirect the user to the login page
    return redirect()->route('login');
});


This will log the user out of the application and then redirect them to the login route.


You can also redirect to a named route by using the route() function, like this:

1
return redirect()->route('login');


Or you can redirect to a controller action by using the action() function:

1
return redirect()->action('AuthLoginController@showLoginForm');


Finally, you can also redirect to a URL by using the to() function:

1
return redirect()->to('http://example.com/login');


I hope this helps! Let me know if you have any questions.

by darrion.kuhn , 8 months ago

@jerad 

To redirect after logout in Laravel, you can follow these steps:

  1. Open the app/Http/Controllers/Auth/LoginController.php file.
  2. Locate the logout() method and add the following line of code before the return statement:
1
2
3
4
protected function loggedOut(Request $request)
{
    return redirect('/your-redirect-url');
}


Replace /your-redirect-url with the URL you want to redirect to after logout.

  1. Save the file.


Now, whenever a user logs out of your Laravel application, they will be redirected to the specified URL.