我需要针对一个条件抛出一个节流异常,而针对另一个条件抛出阻塞的异常,这与授权被拒绝的方式无关,只要授权失败,就会抛出相同的异常。我了解这应该是这样工作的,我真的不必使用form-request类还是有其他方法来处理此问题吗?
我已经在replyPolicy中发布了用于授权的创建方法,并在replyRequest类中发布了passedAuthorization和failedAuthorization方法
public function create(User $user)
{
if (auth()->user()->blocked) {
$this->deny('Your account has been blocked. Contact administrator for further information.');
}
if (!$lastReply = $user->fresh()->lastReply) {
return true;
}
return !$lastReply->wasJustPublished();
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return Gate::allows('create', Reply::class);
}
/**
* Handle a failed authorization attempt.
*
* @return void
*
* @throws ThrottleException
*/
protected function failedAuthorization()
{
throw new ThrottleException(
'You are replying too frequently. Please take a break.', 429
);
}
我想显示一个关于用户是否被阻止的响应,而不同的响应用于用户是否过于频繁的答复。