我正在尝试使用Laravel Polices和Gate实现ACL
。以下是我的代码。
UserPolcy:
class UserPolicy
{
use HandlesAuthorization;
public function isRoleAllowed(User $user, $role)
{
return in_array( $role , $user->getRoles());
}
}
AuthServiceProvider:
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
];
public function boot()
{
$this->registerPolicies();
// User Policy
foreach ( get_class_methods(new \App\Policies\UserPolicy) as $method ) {
Gate::define($method, "App\Policies\UserPolicy@{$method}");
}
}
}
我如何尝试在我的控制器中访问isRoleAllowed,
Gate::allows('isRoleAllowed', 'APPROVE_INVOICE')
总是回归真实。它没有执行isRoleAllowed函数。我不知道我哪里出错了。