首页 > 解决方案 > Laravel 油门消息

问题描述

我正在使用 ThrottleRequest 来限制登录尝试。在 Kendler.php 我有

'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

和我在 web.php 中的路线

Route::post('login', ['middleware' => 'throttle:3,1', 'uses' => 'Auth\LoginController@authenticate']);

当我第四次登录时,它返回状态 429 并带有消息“请求过多”。(默认情况下我猜)
但我只想返回错误消息,例如:

return redirect('/login')
            ->withErrors(['errors' => 'xxxxxxx']);

任何人帮助我!谢谢你!

标签: laravelthrottling

解决方案


您可以扩展中间件并覆盖该buildException()方法以更改它在抛出 a 时传递的消息,ThrottleRequestsException或者您可以使用异常处理程序来捕获ThrottleRequestsException并执行您想要的任何操作。

所以Exceptions/Handler.php你可以做类似的事情

use Illuminate\Http\Exceptions\ThrottleRequestsException;

public function render($request, Exception $exception)
{
    if ($exception instanceof ThrottleRequestsException) {
      //Do whatever you want here.
    }

    return parent::render($request, $exception);
}

推荐阅读