所以我想尝试使用Laravel Authorization
。对于here我做了以下:
我创建了一个策略并在其中放入以下代码:
//Allow users of type 'users`
public function view(User $user)
{
dd('he was here'); // Not coming here
return $user->user_type === 'user';
}
在控制器中我做了:
if (Gate::denies('view')) {
dd('Sorry Bud not allowed');
}
事情是,它总是进入块'Sorry Bud not allowed'
。
我错过了一些步骤还是别的什么?
用Gate检查2件事
第一:确保您的政策已注册。
第二:如果你在config.auth中设置了自定义Guard,那么在你的控制器中添加以下行。
config(['auth.defaults.guard','YOUR-CUSTOM-GUARD-NAME']);
您可能需要注册策略,请参阅文档http://laravel.com/docs/5.1/authorization#policies中的“注册策略”部分
您还需要将App\Providers\AuthServiceProvider::class,
添加到config / app.php中的providers数组中。
这只是因为您没有登录到您的项目以了解用户。所以首先登录你的项目然后这个代码是有效的。