如何将自定义错误消息从 LoginController 发送到前端?
P粉101708623
P粉101708623 2023-09-16 12:57:17
0
1
549

我在 LoginController 中添加了一项检查来限制用户连接设备的最大数量。

我在 LoginControllerlogin() 方法中添加了以下内容:

public function login(Request $request)
{
    // ... some code ...

    if ($this->attemptLogin($request)) {
        $user = Auth::user();
        if ($user->max_devices >= 5) {
            // if I dd() instead of returning this, it gets here
            return $this->sendMaxConnectedDevicesResponse($request);
        }
    }

    // ... some code ...
}

protected function sendMaxConnectedDevicesResponse(Request $request)
{
    throw ValidationException::withMessage([$this->username() => ['Device limit reached'])->status(403);
}

sendMaxConnectedDevicesResponse 是带有我的自定义消息的 sendLockoutResponse 的副本,但是我收到警告,提示我有未处理的异常 (Unhandled \Illuminate\Validation\ValidationException< /代码>)。

那么我该如何像 sendLockoutResponse 处理它一样处理它,这样它就会在前端显示为错误,而不是仅仅忽略它?现在,发生的情况是,即使它抛出错误,它也不会在前端显示它,并且继续照常登录

我只是没有找到正确抛出和捕获自定义错误的方法

P粉101708623
P粉101708623

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!