Laravel form表单中认证失败如何返回自定义消息?
Laravel6.x 请问当authorize方法返回false时,如何自定义消息?
class AmazonOrderRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
Log::debug($this->route('access_id'));
//アクセス認証
$api_authority = DB::table('api_authentication')->where('api_name', 'HomePage')->first();
if (!$api_authority || $api_authority->access_id != $this->json()->get('access_id','') ) {
return false;
}
return true;
}
}
在当前表单请求验证类中 重写父类 FormRequest 的 failedAuthorization 方法
或者在 App\Exceptions\Handler.php 中捕捉 FormRequest 抛出的 AuthorizationException 异常
建议第一种就可以