Laravel 5.4 - 将 AuthServiceProvider 绑定到路由组?
Laravel 5.4 - Bind AuthServiceProvider to route group?
我的路线分为 2 组:web
(默认)组和 admin
(自定义)组。 admin
组使用 auth
中间件。其他一切都使用 web
中间件。
我遇到一个问题,我的 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
中间件中的每个请求中,我的应用程序都会在不需要时查询用户的权限。 不需要 在我的网络中间件中查询任何权限。那么我如何告诉我的 AuthServiceProvider 只检查我的 Auth
组(或中间件)内路由的权限?
我刚刚从使用服务提供商切换到使用我自己的自定义中间件
我的路线分为 2 组:web
(默认)组和 admin
(自定义)组。 admin
组使用 auth
中间件。其他一切都使用 web
中间件。
我遇到一个问题,我的 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
中间件中的每个请求中,我的应用程序都会在不需要时查询用户的权限。 不需要 在我的网络中间件中查询任何权限。那么我如何告诉我的 AuthServiceProvider 只检查我的 Auth
组(或中间件)内路由的权限?
我刚刚从使用服务提供商切换到使用我自己的自定义中间件