gpt4 book ai didi

php - Laravel 5 如何显示节流消息,如 session 闪存?

转载 作者:行者123 更新时间:2023-12-02 17:20:06 25 4
gpt4 key购买 nike

我使用的是 laravel 5.4,我想像下面这样限制路由的请求

Route::group(['middleware' => ['throttle:2']], function () {

Route::post('/xxx', 'TestController@getTest');

});

它运行良好,但收到“尝试次数过多”时。它显示在空白页上。有没有办法在 Blade View 中显示类似 session 闪存消息?

最佳答案

因此,一种简单的方法是更改​​节流中间件。

首先,创建一个新的中间件,它像这样扩展基本节流中间件:

namespace App\Http\Middleware;

use Illuminate\Routing\Middleware\ThrottleRequests as
BaseThrottleRequests;

class ThrottleRequests extends BaseThrottleRequests
{
}

然后在 app/Http/Kernel.php 中更改节流中间件:

'throttle' => \App\Http\Middleware\ThrottleRequests::class

它现在将使用您自己的 throttle 中间件,并且由于它是从 laravel 的中间件扩展而来的,因此它具有自己的功能并且像以前一样工作。

然后,查看基类内部,您会发现 buildResponse 会在出现 Too Many Attemps 情况时构建响应。因此,您只需要在中间件中覆盖它:

protected function buildResponse($key, $maxAttempts)
{
$retryAfter = $this->limiter->availableIn($key); // This gives you the number of seconds before the next time it is available

return redirect('test')->with('error', '...'); // You can use redirect() and all those stuffs as you would normally do to redirect the user and set a session message
}

关于php - Laravel 5 如何显示节流消息,如 session 闪存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43518541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com