我正在编写大量 API 来获取和存储数据。
我喜欢默认的 throttle
选项:
protected $middlewareGroups = [
'api' => [
'throttle:60,1',
'bindings',
],
];
将请求限制为每分钟 60 个;但是对于某些路线(es:POST
),我想增加这个值。
我尝试设置 'throttle:500,1'
在路由中间件如下:
Route::group(function () {
Route::get('semaphore/1', ['uses' => 'App\Api\V1\DBs\SemaphoreController@index']);
Route::post('semaphore/1', ['uses' => 'App\Api\V1\DBs\SemaphoreController@store', 'middleware' => 'WriteToDatabaseMiddleware', 'throttle:500,1']);
});
但它不起作用。
有什么想法吗?
谢谢。
更新:
我注意到 'throttle:500,1'
用于 api.php
路由将在默认 'throttle:60,1'
之后设置指定为 Kernel.php
文件;然后,它不起作用。
记录流程执行,第一个调用是:
Illuminate\Routing\Middleware\ThrottleRequests -> handle
来自 Kernel.php
有maxAttempts=60
.
那么,第二个调用是:
Illuminate\Routing\Middleware\ThrottleRequests -> handle
来自 api.php
有maxAttempts=500
.
换句话说,throttle:500,1
在api.php
文件不要覆盖 throttle:60,1
在Kernel.php
文件。
最佳答案
当前答案
根据this GitHub issue , throttle 中间件不应该被“两次”使用(就像你想那样做)。如何“正确”处理你现在的问题,只有两种方式:
- 编写自己的 throttle 中间件
或
- 为每个路由(组)单独定义 throttle 中间件
旧答案
您设置的中间件 key 错误!声明使用多个中间件时,为它们创建一个新数组
['middleware' => ['WriteToDatabaseMiddleware','throttle:500,1']]
编辑:由于中间件顺序,您应该将内核限制设置为您要使用的最高值,并且所有其他路由都应该有一个降低 throttle 值到相应的值。
关于php - Laravel 5.4 - 覆盖 API 'throttle:60,1',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44202247/