How to change the redirect url when logging out?

后端 未结 8 2039
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 16:08

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

相关标签:
8条回答
  • 2020-12-16 16:29

    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');
        }
    
    0 讨论(0)
  • 2020-12-16 16:32

    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.

    0 讨论(0)
  • 2020-12-16 16:34

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

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

    You get the idea.

    0 讨论(0)
  • 2020-12-16 16:35

    The redirect after logout is hard coded in the trait AuthenticatesAndRegistersUsers. You can override it in your AuthController by adding this:

    public function getLogout()
    {
        $this->auth->logout();
    
        return redirect('logout');
    }
    
    0 讨论(0)
  • 2020-12-16 16:38

    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');
    }
    
    0 讨论(0)
  • 2020-12-16 16:43

    using the built in laravel Auth in the controllers I just override the loggedOut method which triggers after logout to redirect

    in the "LoginController.php" which uses

    use AuthenticatesUsers;
    

    in the AuthenticatesUsers Trait is a logout method, you can optionally override this or you will see that it triggers a loggedOut method

    You can override the logged out method which is default blank and have that redirect

        /**
         * The user has logged out of the application.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return mixed
         */
        protected function loggedOut()
        {
            return redirect()->route('login.show');
        }
    
    0 讨论(0)
提交回复
热议问题