【发布时间】:2021-05-31 15:39:16
【问题描述】:
这是我的 StorePolicy:
public function view(User $user, store $store)
{
return true;
}
,这是我的 AuthServiceProvider:
protected $policies = [
store::class => StorePolicy::class,
];
,这是我的 api.php 文件:
Route::group([ 'middleware' => ['auth:sanctum']],function () {
Route::resources([
'store' => StoreController::class,
]);
});
这是我的控制器:
public function __construct()
{
$this->authorizeResource(store::class, 'store');
}
public function show($id)
{
echo 'hi :)';
}
这是我的问题,当我检查路线('/store')时得到结果,但是当我检查路线('/store/1',.eg)时,我得到 403 错误。我应该怎么办 ? :(
这很奇怪,在我的控制器的索引方法中,当我检查下面的代码时,我得到了我想要的正确结果并且它是否有效。
public function index()
{
$store = new store();
$this->authorize('view', $store);
$data = store::orderBy('created_at', 'desc')->simplePaginate(10);
return response([
'data' => $data,
], 200);
}
【问题讨论】:
-
阅读github.com/laravel/framework/issues/… 有一点是
show($id)必须是show(store $id)例如