提问人:Firas Miladi 提问时间:11/15/2023 最后编辑:Firas Miladi 更新时间:11/16/2023 访问量:27
Laravel 自定义用户模型策略问题
Laravel Custom User Model Policy Issue
问:
我正在开发一个Laravel项目,我有一个自定义用户模型(管理模型)。我正在尝试为 ThirdParty 模型创建策略,但即使从策略方法返回 true,我也始终遇到“操作未经授权”错误。
这是策略方法:
public function view(?Admin $admin, ThirdParty $thirdParty)
{
return true;
}
我已在 AuthServiceProvider 中注册了该策略,如下所示: 使用 App\Models\ThirdParty; 使用 App\Policies\ThirdPartyPolicy;
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
ThirdParty::class => ThirdPartyPolicy::class,
];
// ...
}
这就是我在api.php中应用它的方式:
Route::get('show/{id}', [ThirdPartyController::class, 'show'])->can('view', 'thirdParty');
最后,控制器操作如下所示:
public function show(Request $request, ThirdParty $thirdParty)
{
// ...
}
我试过die();在策略方法中,但它没有用 我想要解决我的问题
答:
0赞
Firas Miladi
11/16/2023
#1
我找到了一个解决方案,我刚刚删除了这个:
->can('view', 'thirdParty')
from 并将其替换为api.php
$this->authorize('view', ThirdParty::class);
在控制器操作中,它对我有用!
评论