在我的情況下,請求授權將引發AccessDeniedException錯誤
class IndexRequest extends FormRequest
{
public function authorize()
{
return auth()->user()->can('list-foos');
}
}
由此,在索引函式上路由系結
公共函式索引(IndexRequest $request)
在處理程式類中,
$this->reportable(function (AccessDeniedHttpException $e) {
return $this->response('unauthorized', 403);
});
但
“訊息”:“此操作未經授權。”,“例外”:“Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException”,“檔案”:“/var/www/html/vendor/laravel/framework/src/Illuminate/ Foundation/Exceptions/Handler.php", "line": 356,
被拋出作為回應
我試圖從 Spatie 檔案中捕獲例外
https://spatie.be/docs/laravel-permission/v5/advanced-usage/exceptions
uj5u.com熱心網友回復:
發現。我必須使用render()函式而不是register()和
照亮\Auth\Access\AuthorizationException
public function render($request, Throwable $exception)
{
if ($exception instanceof AuthorizationException) {
return $this->responseUnauthroized('Unauthorized');
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/458705.html
上一篇:多次嘗試塊
