2017-12-18 18 views
-2

我想爲特定用戶驗證視圖文件。我爲此創建了中間件。以下是中間件的代碼: -創建中間件,但它不在Laravel 5.5中工作

namespace App\Http\Middleware; 

use Closure; 

use Illuminate\Support\Facades\Auth; 

class ClientAuth 
{ 
    public function handle($request, Closure $next) 
    { 
     if(Auth::user() && Auth::user()->account_type==3) 
     { 
      return $next($request); 
     } 

      return redirect('/'); 

    } 
} 

接下來,我在kernel.php文件中調用了'客戶端'中間件。以下是代碼: -

protected $routeMiddleware = [ 
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class, 
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 
    'can' => \Illuminate\Auth\Middleware\Authorize::class, 
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 
    'admin' => \App\Http\Middleware\isAdmin::class, 
    'client' => \App\Http\Middleware\ClientAuth::class 
]; 

然後,我已經在路由中調用了它。

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

    Route::get('clientdatabase', 
    'ClientDatabase\[email protected]'); 

}); 

這是行不通的。我哪裏錯了?

+3

「不工作」不是什麼hapening ......你有什麼期望發生的說明,什麼是真正發生 – lagbox

+0

你不需要身份驗證的中間件 – madalinivascu

+0

如果我刪除AUTH中間件,它不起作用 –

回答

-2

您的呼叫在路由中是錯誤的。 例如

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

     Route::get('clientdatabase','ClientDatabase\[email protected]'); 

    }); 
+0

這只是一種替代方式註冊組 – lagbox

+0

謝謝!但仍然沒有改變。 –

+1

那麼,仍然「不工作」,對吧? – Amarnasan

相關問題