Tim van Uum Tim van Uum - 1 month ago 102
PHP Question

How to set laravel 5.3 logout redirect path?

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.

Answer

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.