2015-05-19 166 views
2

我在這裏虧本。更改後登錄重定向路徑

我已經設置了內置身份驗證的默認安裝laravel。我將home路線改爲admin。我更新了RedirectIfAuthenticated.php這個變化。

基本上,我要的是爲用戶被重定向到指數(/)第一次登陸,

我已經沖刷谷歌和嘗試添加protected $redirectTo = '/'protected $redirectPath = '/'app/Http/Controllers/Auth/AuthController.php和似乎沒有任何改變重定向。

如果我將new RedirectResponse(url('/admin'));更改爲new RedirectResponse(url('/'));RedirectIfAuthenticated.php然後我得到一個重定向循環。

+0

請參閱http://stackoverflow.com/questions/28702372/how-to-change-default-redirect-url-of-laravel-5-auth-filter –

+0

@KhanShahrukh這是如何改變重定向_to_登錄頁面。我想重定向用戶_after_他們已登錄到索引頁面,而不是默認的「家」(我已重命名爲管理員)頁面。 – gin93r

+0

你對我的評論很生氣,我問了,因爲我覺得它 –

回答

3

答案如下。如果已經通過身份驗證,用戶將從登錄屏幕重定向,並且目標位於中間件中。

例如,默認登錄位置當前設置爲/ home,但我們會更改它。

編輯位於app/Http/Middleware目錄中的RedirectIfAuthenticated.php文件。將第38行更改爲以下內容。

更改默認的位置 線#38記錄應

return new RedirectResponse('/home'); 

//將其更改爲

return new RedirectResponse('/admin/post'); 

...或者其他任何地方你想要的頁面重定向到。

1

添加

protected $redirectPath = '/dashboard'; 

在控制器\驗證\ AuthController

0

最初從this Laracasts post拍攝。您可以相應地編輯App\Http\Controllers\Auth\LoginController中的authenticated方法。

在我的情況:

public function authenticated() 
{ 
    $user = $this->getUser(); 

    if ($user && $user->isAdmin()) { 
     $this->redirectTo = route('admin.users.index'); 

     return null; 
    } 
} 

return null線是因爲AuthenticateUsers特徵實現了sendLoginResponse(Request $request)方法,並詢問是否authenticate方法返回的東西:

return $this->authenticated($request, $this->guard()->user()) 
      ?: redirect()->intended($this->redirectPath()); 

或執行重定向。

你可以不返回任何東西,它會有同樣的效果。