2016-06-14 136 views
1

我一直在努力從這個安靜的時間,現在我正在嘗試的是重定向所有url的未登錄用戶登錄頁面的命中,它給了我這個錯誤,這我確定是因爲它在/ login URL上創建了一個循環。身份驗證也在登錄頁面中檢查授權用戶。但是我希望登錄頁面在檢查身份驗證時應該是一個例外。我可能做錯了我無法得到的東西。這裏是我的代碼。Laravel登錄重定向你太多

routes.php文件

Route::post('login', 'Auth\[email protected]'); 
Route::get('login' , 'Auth\[email protected]'); 
Route::get('/'  , 'Auth\[email protected]'); 

kernel.php

protected $routeMiddleware = [ 
    'auth' => \App\Http\Middleware\Authenticate::class, 
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 
    'can' => \Illuminate\Foundation\Auth\Access\Middleware\Authorize::class, 
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 
    'acl' => \App\Http\Middleware\CheckPermission::class, 
]; 

身份驗證類

class Authenticate 
{ 
    public function handle($request, Closure $next, $guard = null) {  
     if (Auth::guard($guard)->guest()) { 
     if ($request->ajax() || $request->wantsJson()) { 
      return response('Unauthorized.', 401); 
     } else { 
      return redirect()->guest('login'); 
     } 
     } 
    return $next($request); 
    } 
} 

AuthController類

class AuthController extends Controller { 

    use AuthenticatesAndRegistersUsers, ThrottlesLogins; 
    protected $redirectTo = '/dashboard'; 
    protected $loginPath = '/login'; 
    protected $redirectPath = '/dashboard'; 
public function __construct(){ 
    $this->middleware('auth', ['except' =>'login']); 
    /* I have been trying these many things to fix this, all in loss. 
    // $this->middleware('acl'); // To all methods 
    // $this->middleware('acl', ['only' => ['create', 'update']]); 
    // $this->middleware('guest', ['only' => ['/login']]); 
    // echo "Fuck it"; exit; 
    // $this->middleware('auth'); 
    // $this->middleware('auth', ['only' => ['login']]); 
    // $this->middleware('auth', ['only' => ['/login']]); 
    // $this->middleware('auth', ['except' => 'login']); 
    // $this->middleware('guest'); 
    // $this->middleware('guest', ['only' => ['logout' , 'login', '/login', '/']]); 
} 

請幫助我,這一切會在我頭上,好像某種火箭科學的給我。順便說一句,我是拉拉維爾新手,可能會做一些愚蠢的事情,爲此道歉。提前致謝。

回答

1

爲什麼你這樣做只是爲了將每個未登錄的用戶重定向到登錄表單?

我認爲你可以做到這一點

routes.php文件

Route::post('login', 'Auth\[email protected]'); 
    Route::get('login' , 'Auth\[email protected]'); 
    Route::get('/'  , 'Auth\[email protected]'); 

Route::group(['middleware' => 'auth'], function() { 

    // any route here will only be accessible for logged in users 
}); 

,並權威性控制器的結構應該是這樣的

AuthController

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

感謝您的回覆,我會在一段時間內嘗試這個,我知道我在做這一切都很麻煩,但是你能告訴我我在哪裏創建一個循環。 – ScoRpion

+0

只是像我給你看的那樣,它應該很好地工作 –

+0

我做了你所提到的,它給了我同樣的錯誤,它不能解決priblem。我刪除了我的路線,並把你的,也取代了我的AuthController構造,但沒有運氣.. – ScoRpion

1

問題在於你的路線。

當我輸入並且我沒有註銷時,您發送我登錄(獲取)路線。當您在AuthController的構造函數中指定中間件時,每次調用AuthController的一個方法時,構造函數都會被再次調用,並在登錄時發回給您,並且它會無限重複。

+0

所以我如何解決這個問題,我知道我做錯了什麼? – ScoRpion

3

您需要添加路由登錄外Laravel組:

routes.php文件

Route::auth(); 

Route::group(['middleware' => 'auth'], function() { 
    // All route your need authenticated 
}); 

的方法,另外,你可以用看你的路由表:

php artisan route:list 
+0

這應該是被接受的答案。它正是我所需要的,乾淨而簡單,就像拉拉維爾所想的那樣。 –