How to change the redirect url when logging out?

2020-02-10 03:42发布

I'm working with Laravel 5 authentification system provided by default. After logging out, a user is redirected to the root page but I'd like to change that. I managed to do it for the "login" and "registering" process by defining "$redirectTo" in "AuthController.php". But for "logout", I defined "$redirectAfterLogout" at the same place but it seems to not be taken into account.

Could anyone explain me where is the problem and how to fix it please? Thanks a lot.

8条回答
放荡不羁爱自由
2楼-- · 2020-02-10 04:18

In App\Controllers\Auth\AuthController, add the following two variables.

protected $redirectTo = '/private_dashboard';
protected $redirectAfterLogout = '/public_homepage';

You get the idea.

查看更多
Summer. ? 凉城
3楼-- · 2020-02-10 04:23

For Laravel 5.5 override logout method inside LoginController. In my case I am redirecting to home route after login.

/**
 * Log the user out of the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function logout(Request $request)
{
    $this->guard()->logout();
    $request->session()->invalidate();

    return redirect()->route('home');
}
查看更多
We Are One
4楼-- · 2020-02-10 04:23

Add this to your route

Route::get('logout', function(){ Auth::logout(); return redirect('/'); /Added this line. The auth doesn't seem to work on its own/ });

查看更多
SAY GOODBYE
5楼-- · 2020-02-10 04:24

For Laravel 5,

Open AuthController class : app/Http/Controllers/Auth/AuthController.php

Add below property to the class

protected $redirectAfterLogout = 'auth/login';

you can change auth/login with any url.

查看更多
淡お忘
6楼-- · 2020-02-10 04:25

it'only laravel versi 5.4 if you want custom redirect url logout, open /your-project-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php and edit redirect based on you needed

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

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

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

        return redirect('/login');
    }
查看更多
爷、活的狠高调
7楼-- · 2020-02-10 04:26

I have a same problem in Laravel 5.0. Override a method does the trick.

1) Go to app/Http/Controllers/Auth/AuthController.php 2) Add a new method :

// Override Logout method (define custom url)
public function getLogout()
{
    $this->auth->logout();
    return redirect('auth/login');  // Your Custom URL
}
查看更多
登录 后发表回答