Laravel是怎么处理ValidationException的和怎么通知到界面上显示的
问题描述?
在看官方的Laravel Breeze项目
,为什么认证的时候抛出一个异常,界面上可以接收到这个异常信息并显示呢
下面是当点击登录去认证的代码
public function authenticate()
{
$this->ensureIsNotRateLimited();
if (! Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([
'email' => trans('auth.failed'),
]);
}
RateLimiter::clear($this->throttleKey());
}
对应的视图, 当认证的时候抛出一个异常的时候,为什么下面的x-auth-validation-errors
就会收到相应的错误信息?
<x-guest-layout>
<x-auth-card>
<x-slot name="logo">
<a href="/">
<x-application-logo class="w-20 h-20 fill-current text-gray-500" />
</a>
</x-slot>
<!-- Session Status -->
<x-auth-session-status class="mb-4" :status="session('status')" />
<!-- Validation Errors -->
<x-auth-validation-errors class="mb-4" :errors="$errors" />
...
找到处理的相关代码了:
ShareErrorsFromSession
类中的handle
方法负责从session中获取错误信息,并放在errors变量中捕获ValidationException 异常是在
Handler
类中把相关错误信息放在session中 RedirectResponse类