2017-08-16 46 views
1

我有以下文件:Laravel 5.4中間件不能定義後衛

routes.php文件

Route::get('client-portal', '[email protected]'); 

DashboardController.php

public function __construct() 
{ 
    $this->middleware('auth:client-users'); 
} 

auth.php

'guards' => [ 
    'web' => [ 
     'driver' => 'session', 
     'provider' => 'users', 
    ], 
    'client-users' => [ 
     'driver' => 'session', 
     'provider' => 'client-users', 
    ], 
    'api' => [ 
     'driver' => 'token', 
     'provider' => 'users', 
    ], 
], 
'providers' => [ 
    'users' => [ 
     'driver' => 'eloquent', 
     'model' => App\Models\User::class, 
    ], 
    'client-users' => [ 
     'driver' => 'eloquent', 
     'model' => \App\Models\ClientPortal\User::class 
    ], 
], 

Authenticate.php

public function __construct(Guard $auth) { 
    $this->auth = $auth; 
} 

public function handle($request, Closure $next) 
{ 
    if ($this->auth->guest()) { 
     if ($request->ajax()) { 
      return response('Unauthorized.', 401); 
     } else { 
      dd($this->auth); 
      return redirect()->guest('login'); 
     } 
    } 

    return $next($request); 
} 

每當我通過client-users登錄並瀏覽到/client-portal.Authenticate.phpdd返回一個SessionGuard實例,與屬性name設置爲web

但是,我並指定「身份驗證:客戶端用戶在控制器的構造函數中。 所以我的第一個想法是認證中間件是全球中間件,所以我檢查了它,但事實並非如此。如果我從構造函數中刪除中間件行,則會顯示頁面。

有誰知道問題在哪裏?

謝謝。

回答

0

顯然傳遞到__construct實例不支持:符號用於指定後衛。這Authenticate.php是來自以前版本laravel的原始文件。 guard屬性作爲第三個參數傳遞給handle

我有固定的代碼如下:

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

     return $next($request); 
    } 
}