Laravel 政策正在解决奇怪的问题
Laravel policies are resolving weird
我需要一个在锦标赛中创建树的策略。
所以,在我的 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);
}
我相信 generateTree() 的第一个参数必须是经过身份验证的用户。尝试将其更改为 public function generateTree(User $user, Tree $tree, Tournament $tournament )
编辑:
还将 cannot 方法更改为 Auth::user()->cannot('generateTree', [Tree::class, $tournament])
(将数组中的第二个和第三个参数组合在一起,似乎 Laravel 总是期待 2 个参数,其中第二个参数可以是数组)
我需要一个在锦标赛中创建树的策略。
所以,在我的 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);
}
我相信 generateTree() 的第一个参数必须是经过身份验证的用户。尝试将其更改为 public function generateTree(User $user, Tree $tree, Tournament $tournament )
编辑:
还将 cannot 方法更改为 Auth::user()->cannot('generateTree', [Tree::class, $tournament])
(将数组中的第二个和第三个参数组合在一起,似乎 Laravel 总是期待 2 个参数,其中第二个参数可以是数组)