所以我试图使用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'
。
我错过了一些其他步骤吗?
答案 0 :(得分:1)
使用Gate检查2件事
首先:确保您的政策已注册。
第二:如果您在config.auth中设置了自定义Guard,那么在控制器内添加以下行。
config(['auth.defaults.guard','YOUR-CUSTOM-GUARD-NAME']);
答案 1 :(得分:0)
您可能需要注册策略,请参阅文档中的“注册策略”部分 http://laravel.com/docs/5.1/authorization#policies
答案 2 :(得分:0)
您还需要将App\Providers\AuthServiceProvider::class,
添加到config / app.php中的providers数组中。
答案 3 :(得分:0)
这仅仅是因为您没有登录到项目来了解用户。 因此,请先登录到您的项目,然后此代码即可使用。