Laravel 无法覆盖策略中的授权方法
我想在授权失败时用自定义消息进行响应。 我已覆盖Policy类中的方法,但它不返回自定义消息 政策:Laravel 无法覆盖策略中的授权方法,laravel,laravel-authorization,Laravel,Laravel Authorization,我想在授权失败时用自定义消息进行响应。 我已覆盖Policy类中的方法,但它不返回自定义消息 政策: class PostPolicy { use HandlesAuthorization; /** * Determine if user can view post * @param User $user * @param Post $post * @return bool */ public functi
class PostPolicy
{
use HandlesAuthorization;
/**
* Determine if user can view post
* @param User $user
* @param Post $post
* @return bool
*/
public function view(User $user, Post $post)
{
return $user
->posts()
->where('post_id', $post->id)
->exists();
}
/**
* [deny description]
* @return [type] [description]
*/
protected function deny()
{
return response()->json([
'message' => 'My custom unauthorized message'
], 401);
}
}
在后置控制器中实现:
...
public function show(Post $post)
{
$this->authorize('view', $post);
...
}
响应仍然返回HandlesAuthorization
trait中定义的内容,即:
protected function deny($message = 'This action is unauthorized.')
{
throw new AuthorizationException($message);
}