How to set laravel 5.3 logout redirect path?

只谈情不闲聊 提交于 2019-12-18 10:16:34

问题


Is there no elegant solution to redirect to a specific page after logging out in Laravel 5.3?

The function being called is from the trait AuthenticatesUsers:

public function logout(Request $request)
{
    $this->guard()->logout();

    $request->session()->flush();

    $request->session()->regenerate();

    return redirect('/');
}

This is a default function from the core of laravel. So I have to override the whole function I cannot edit the core. But isn't there a more simpler solution, cause it feel like overkill to manually logout, flush and regenerate again.

Worked the answers out in an article: https://codeneverlied.com/how-to-set-logout-redirect-path-in-laravel-5-8-and-before/


回答1:


This is how I did it. In Auth\LoginController you have:

use AuthenticatesUsers;

Change it to:

use AuthenticatesUsers {
    logout as performLogout;
}

Then, define a new logout() method in your LoginController:

public function logout(Request $request)
{
    $this->performLogout($request);
    return redirect()->route('your_route');
}

Sure, regular logout() method in that trait has only 3 lines (used to log users out of the system) so you can copy them to your method, but you should always follow the DRY principle (don't repeat yourself) and re-use as much code as you can.




回答2:


Laravel > 5.7

The accepted answer is fine, but you can completely bypass touching any of the logout logic by simply overwriting the loggedOut method:

// App\Http\Controllers\Auth\LoginController.php
protected function loggedOut(Request $request) {
    return redirect('/where/ever/you/want/to/go');
}



回答3:


I would inherit LoginController and override the logout function coming from the trait in there:

LoginController.php -> leave that as it is.

MyLoginController.php:

class MyLoginController extends LoginController {

protected $redirectAfterLogout = '/goodbye';

    public function logout(Request $request)
    {
        $this->guard()->logout();
        $request->session()->flush();
        $request->session()->regenerate();
        return redirect($this->redirectAfterLogout);
    }
}

Of course, you should remember to update your Auth routes accordingly.




回答4:


I'm using Laravel-5.2, what I used was:

public function logout()
{
    Auth::logout();
    Session::flush();
    return redirect('/');
}

Make sure you have imported:

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

In your controller.




回答5:


The Auth::routes method in laravel 5.3 registers a POST route for /logout instead of a GET route. This prevents other web applications from logging your users out of your application. To upgrade, you should either convert your logout requests to use the POST verb or just register your own GET route for the /logout URI by adding this route to the file Routes/web.php:-

Route::get('/logout', 'Auth\LoginController@logout');

and it should work fine and redirect you to the '/' directory as it's defined in the LoginController.php

Quoted from:-

https://laravel.com/docs/5.3/upgrade




回答6:


Assuming someone is viewing it now a days and the version of the laravel they are using is 5.7

Add this line in LoginController.php

public function logout()
{
    Auth::logout();
    return redirect()->to('/your-route');
}

This assumes you are using the out of the box authentication module provided by laravel




回答7:


Every logout action fires an event Events\Logout. You can create a listener that listens to this event and add some logic to there. See more about listeners here https://laravel.com/docs/5.3/events




回答8:


The simplest way is to override logout trait at LoginController in App\Http\Controllers\Auth\LoginController like this

public function logout(Request $request){
    $this->guard()->logout();
    $request->session()->flush();
    $request->session()->regenerate();

    return redirect()->route('you_route_name');
}



回答9:


If you're using the out of the box AuthController add this variable to the top and then change the string to redirect wherever you want.

protected $redirectAfterLogout = '/';

The AuthenticatesUsers class has a logout function that checks for this variable.

public function logout()
{
        Auth::guard($this->getGuard())->logout();

    return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
}



回答10:


Just use this in routes/web.php

Route::get('logout', function (){
Auth::logout();
return redirect('your URL');
});



回答11:


In Laravel 5.8 find the below path :

App\Http\Controllers\Auth\LoginController.php

use Illuminate\Http\Request;*

write this function

public function logout(Request $request){
    $this->guard()->logout();

    $request->session()->invalidate();

    return $this->loggedOut($request) ?: redirect('/Where/You/want/to/redirect');;
}



回答12:


To avoid repeating logout code and to follow DRY, you may

  • Add a custom logout route in Auth/LoginController, call Auth::logout() and return redirect to your path, or
  • Add an after-middleware (say redirectAfterLogout) and add it to the logout route



回答13:


The accepted answer is fine, but you can completely bypass touching any of the logout logic by simply overwriting the loggedOut method:

protected function loggedOut(Request $request) {
    return redirect('/where/ever/you/want/to/go');
}



回答14:


you can go to vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php in function logout change return redirect('/'); to your route address.



来源:https://stackoverflow.com/questions/39327970/how-to-set-laravel-5-3-logout-redirect-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!