2016-01-02 55 views
2

嘗試使用laravel默認控制器(AUTH /口令)來實現我的網站上簡單的用戶註冊/登錄功能,但只要我登錄,類RedirectIfAuthenticated手柄功能可以防止所有訪問驗證網址的,因此我不能註銷了。是否有一個錯誤,我需要寫在手柄功能異常或有我錯過了什麼? 這裏是一流的樣子默認:Laravel認證的用戶註銷錯誤

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); 
    } 
} 

回答

4

AuthController的構造應類似於此:

public function __construct() 
{ 
    $this->middleware('guest', ['except' => 'logout']); 
} 

guest中間件由RedirectIfAuthenticated類和處理,以有註銷功能的工作,你應該選擇一個:

  • AuthController調用logout方法。
  • 調用哪種方法你AuthController的構造函數用於註銷和排除:

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

該解決方案適用於Laravel 5+。如果您使用Laravel,請考慮使用過濾器而不是中間件。我認爲解決方案應該是一樣的。 – whoan

+0

感謝您的解釋! :) – MattJ

+0

這完全由注重我的''except''關鍵的幫助。我的控制器行動是'getLogout()',所以中間件是防止註銷。將中間件更改爲'getLogout'併成功! – JoshP