【问题标题】:Laravel Throttle messageLaravel 油门消息
【发布时间】:2019-03-16 05:50:28
【问题描述】:

我正在使用 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']);

谁能帮帮我!谢谢!

【问题讨论】:

    标签: laravel throttling


    【解决方案1】:

    您可以扩展中间件并覆盖 buildException() 方法以更改它在抛出 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);
    }
    

    【讨论】:

    • 哦,是的,我覆盖了 buildException 并且它正在工作。非常感谢
    猜你喜欢
    • 2023-02-04
    • 2020-06-01
    • 2020-10-14
    • 1970-01-01
    • 2019-01-22
    • 2020-04-20
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多