【问题标题】:How to change the redirect url when logging out?注销时如何更改重定向网址?
【发布时间】:2015-06-30 02:28:36
【问题描述】:

我正在使用默认提供的 Laravel 5 身份验证系统。 注销后,用户被重定向到根页面,但我想更改它。 我设法通过在“AuthController.php”中定义“$redirectTo”来完成“登录”和“注册”过程。但是对于“注销”,我在同一个地方定义了“$redirectAfterLogout”,但似乎没有考虑在内。

谁能解释一下问题出在哪里以及如何解决? 非常感谢。

【问题讨论】:

    标签: authentication redirect laravel-5


    【解决方案1】:

    在控制器中使用内置的 laravel Auth 我只是覆盖了logout 方法,该方法在注销后触发以重定向

    在“LoginController.php”中使用

    use AuthenticatesUsers;
    

    在 AuthenticatesUsers Trait 中是一个注销方法,您可以选择覆盖它,否则您会看到它触发了一个 loggedOut 方法

    您可以覆盖默认为空白的已注销方法并进行重定向

        /**
         * The user has logged out of the application.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return mixed
         */
        protected function loggedOut()
        {
            return redirect()->route('login.show');
        }
    

    【讨论】:

      【解决方案2】:

      对于 Laravel 5.5 覆盖 LoginController 中的注销方法。在我的情况下,我在登录后重定向到主路由。

      /**
       * 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');
      }
      

      【讨论】:

        【解决方案3】:

        如果你想要自定义重定向 url 注销,它只有 laravel versi 5.4, 打开/your-project-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php 并根据您的需要编辑重定向

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

        【讨论】:

        • 不应提交或编辑供应商文件。这将在您的下一次作曲家更新时中断。
        • 其实是正确的函数,但是错误的文件,在L5.4,你应该把这个方法覆盖到Http/Controllers/Auth/LoginController.php
        【解决方案4】:

        我在 Laravel 5.0 中遇到了同样的问题。覆盖一个方法就可以了。

        1) 转到 app/Http/Controllers/Auth/AuthController.php 2) 添加新方法:

        // Override Logout method (define custom url)
        public function getLogout()
        {
            $this->auth->logout();
            return redirect('auth/login');  // Your Custom URL
        }
        

        【讨论】:

          【解决方案5】:

          在 App\Controllers\Auth\AuthController 中,添加以下两个变量。

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

          你明白了。

          【讨论】:

            【解决方案6】:

            如果您不提供$redirectAfterLogout 属性,它将使用默认值'/'

            这个逻辑可以在这个类中找到:\vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php

            public function logout()
            {
                Auth::guard($this->getGuard())->logout();
            
                return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
            }
            

            话虽如此,只需在您的 AuthController 中添加此属性:

            protected $redirectAfterLogout = '/afterRedirectURL';
            

            【讨论】:

              【解决方案7】:

              对于 Laravel 5,

              打开AuthController类:app/Http/Controllers/Auth/AuthController.php

              将以下属性添加到类中

              protected $redirectAfterLogout = 'auth/login';
              

              您可以使用任何网址更改auth/login

              【讨论】:

              • 这很奇怪。我已经尝试过了,但没有成功……但是当我几分钟前再次尝试时,它起作用了。谢谢!
              • @kururin 那是因为它是最近才添加的...github.com/laravel/framework/commit/…
              • 我如何在 Laravel 5.3 中做到这一点,它不包含 AuthController.php
              • @SunnyKumar,我也有同样的问题。你找到解决方案了吗?
              • @SunnyKumar,我找到了解决方案,请查看这个问题的答案:stackoverflow.com/questions/40114406/…
              【解决方案8】:

              注销后的重定向硬编码在特征AuthenticatesAndRegistersUsers 中。您可以通过添加以下内容在 AuthController 中覆盖它:

              public function getLogout()
              {
                  $this->auth->logout();
              
                  return redirect('logout');
              }
              

              【讨论】:

              • 是的,那是我的第一次尝试。它有效,但我认为有更好的方法来做到这一点。谢谢!
              猜你喜欢
              • 2020-11-12
              • 2011-07-04
              • 2013-11-09
              • 1970-01-01
              • 2017-06-01
              • 1970-01-01
              • 2013-09-05
              • 2012-09-12
              • 2016-07-01
              相关资源
              最近更新 更多