【发布时间】:2017-01-24 21:47:31
【问题描述】:
我需要一个在锦标赛中创建树的策略。
所以,在我的 treeController@store 中,我有:
if (Auth::user()->cannot('generateTree', new Tree(),$tournament)) {
throw new AuthorizationException();
}
而我相应的政策是:
TreePolicy.php:
public function generateTree(Tree $tree, Tournament $tournament )
{
dd($tournament);
return ($tournament->user_id == Auth::user()->id);
}
我得到一个:
Type error: Argument 1 passed to App\Policies\TreePolicy::generateTree() must be an instance of App\Tree, instance of App\User given, called in /laravel/vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php on line 382
我错过了什么???
编辑:响应@andonovn,
我试过了:
public function store(User $user, Tree $tree, Tournament $tournament)
{
dd($tournament);
}
还有
if (Auth::user()->cannot('generateTree', Tree::class,$tournament)) {
throw new AuthorizationException();
}
--> 它给了我一个 403
或者
$this->authorize('store', $tournament,Tree::class);
--> 没有进入dd();
我发现它起作用的唯一方法是将策略内容放在不太好的控制器中:
if ($tournament->user_id != Auth::user()->id){
throw new AuthorizationException();
}
编辑 2:我用它来解决它:
在控制器中:
if (Auth::user()->cannot('store', [Tree::class,$tournament])) {
throw new AuthorizationException();
}
在政策中
public function store(User $user, Tournament $tournament)
{
return ($tournament->user_id == $user->id);
}
【问题讨论】: