2017-03-02 129 views
0

我的路線分爲2組:web(默認)組和admin(自定義)組。 admin組使用auth中間件。其他一切都使用web中間件。Laravel 5.4 - 將AuthServiceProvider綁定到路由組?

我遇到問題,我的AuthServiceProvider在兩個組中查詢我的權限模型......但我只想在用戶請求訪問我的admin組內的路由時查詢我的權限模型。這裏是我的AuthServiceProvider:

<?php 

namespace App\Providers; 

use App\Models\Permission; 
use Illuminate\Support\Facades\Gate; 
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; 

class AuthServiceProvider extends ServiceProvider 
{ 
/** 
* The policy mappings for the application. 
* 
* @var array 
*/ 
protected $policies = [ 
    'App\Model' => 'App\Policies\ModelPolicy' 
]; 

/** 
* Register any authentication/authorization services. 
* 
* @return void 
*/ 
public function boot() 
{ 
    $this->registerPolicies(); 

    Gate::before(function($user){ 
     if($user->isAdmin()){ 
      return true; 
     } 
    }); 

    foreach ($this->getPermissions() as $permission) { 
     Gate::define($permission->name, function ($user) use ($permission) { 
      return $user->hasRole($permission->roles); 
     }); 
    } 
} 

protected function getPermissions() 
{ 
    return Permission::with('roles')->get(); 
} 

因爲當它並不需要用戶因此,在我web中間件內的每一個要求,我的應用程序查詢權限。沒有需要在我的web中間件中獲取任何權限來查詢。那麼如何告訴我的AuthServiceProvider檢查我的Auth組(或中間件)中的路由的權限?

回答