2017-08-13 58 views
0

我已經在laravel 5中定義了一箇中間件。但是,當我登錄後,它將我重定向到儀表板,但儀表板不顯示併發生錯誤。我已經上傳了一張照片。 http://imgur.com/a/1fCwtLaravel 5中間件不能正常工作

AdminAuth.php

<?php 
namespace App\Http\Middleware; 
use Closure; 
use Auth; 
class AdminAuth 
{ 
    /** 
    * Handle an incoming request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Closure $next 
    * @return mixed 
    */ 
    public function handle($request, Closure $next) 
    { 
     if(Auth::check()) 
     { 
      $user = Auth::user(); 
      return redirect()->route('admin_display_dashboard'); 
     } else { 
      return redirect()->route('admin_display_login'); 
     } 
    } 
} 

Kernel.php

<?php 
namespace App\Http; 
use Illuminate\Foundation\Http\Kernel as HttpKernel; 
class Kernel extends HttpKernel { 
    protected $middleware = [ 
     \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, 
    ]; 
    protected $middlewareGroups = [ 
     'web' => [ 
      \App\Http\Middleware\EncryptCookies::class, 
      \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, 
      \Illuminate\Session\Middleware\StartSession::class, 
      \Illuminate\View\Middleware\ShareErrorsFromSession::class, 
      \App\Http\Middleware\VerifyCsrfToken::class, 
     ], 
     'api' => [ 
      'throttle:60,1', 
     ], 
    ]; 
    protected $routeMiddleware = [ 
     'auth' => \App\Http\Middleware\Authenticate::class, 
     'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 
     'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class, 
     'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 
     'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 
     'adminAuth' => \App\Http\Middleware\AdminAuth::class, 
    ]; 
} 

Route.php

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

Route::get('admin/dashboard', array('as'=>'admin_display_dashboard', 'uses'=>'[email protected]')); 

}); 
+0

頁說,你已被重定向的次數太多。我認爲你陷入了一個無限循環。這些是中間件非常常見的錯誤。仔細檢查你的條件,看看是否有任何改變。 – Gokigooooks

回答

1

什麼在你的admin_display_dashboard()和admin_display_login(),你肯定有在那裏沒有重定向。

同時嘗試這

if(!Auth::check()) 
{ 

    return redirect()->route('admin_display_login'); 
} 

$user = Auth::user(); 

return $next($request);