Laravel已验证用户注销错误

Laravel已验证用户注销错误,laravel,authentication,logout,Laravel,Authentication,Logout,尝试使用laravel默认控制器(auth/password)在我的站点上实现一个简单的用户注册/登录功能,但一旦我登录,类RedirectIfAuthenticated handle函数将阻止所有对auth url的访问,因此我无法再注销。是否有bug,我需要在handle函数上写一个异常,或者我遗漏了什么? 下面是默认情况下该类的外观: class RedirectIfAuthenticated { /** * Handle an incoming request.

尝试使用laravel默认控制器(auth/password)在我的站点上实现一个简单的用户注册/登录功能,但一旦我登录,类RedirectIfAuthenticated handle函数将阻止所有对auth url的访问,因此我无法再注销。是否有bug,我需要在handle函数上写一个异常,或者我遗漏了什么? 下面是默认情况下该类的外观:

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        //dd($next($request));
        if (Auth::guard($guard)->check()) {
            return redirect('/articles');
        }

        return $next($request);
    }
}

AuthController
的构造函数应类似于以下内容:

public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}
guest
中间件由
RedirectIfAuthenticated
类处理,为了使注销功能正常工作,您应该选择一个:

  • AuthController
    调用
    logout
    方法
  • 调用用于注销的任何方法,并将其排除在
    AuthController
    的构造函数中:

    public function __construct()
    {
        $this->middleware('guest', ['except' => '<whichever_method>']);
    }
    
    public function\uuuu construct()
    {
    $this->middleware('guest',['except'=>'');
    }
    

    • 出于可能更高级的原因和需要,我将展示一个不同的想法

      在任何中间件中,一个人都可以实现自己的
      ,除了
      列表。以下是参考资料:

      <?php
      
      namespace App\Http\Middleware;
      
      use Closure;
      
      class CustomThing
          protected $except = [
              'api/logout',
              'api/refresh',
          ];
      
          public function handle($request, Closure $next)
          {
              foreach ($this->except as $excluded_route) {
                  if ($request->path() === $excluded_route) {
                      \Log::debug("Skipping $excluded_route in this middleware...");
      
                      return $next($request);
                  }
              }
      
              \Log::debug('Doing middleware stuff... '. $request->url());
      
          }
      
      }
      

      如果您希望它是一个可重用的解决方案,请将该匹配算法作为一种特性,并将其导入到您想要排除路由的任何中间件中。

      此解决方案适用于Laravel 5+。如果您使用的是Laravel,请考虑使用过滤器而不是中间件。我想答案应该是一样的。谢谢你的解释!:)这完全有助于我将注意力集中在
      'except'
      键上。我的控制器操作是
      getLogout()
      ,因此中间件正在阻止注销。将中间件更改为
      getLogout
      并成功!
          protected $except = [
              'api/logout',
              'api/refresh',
              'foo/*',
              'http://www.external.com/links',
          ];