我正在编写许多API来获取和存储数据。
我喜欢默认的throttle
选项:
我喜欢默认的
throttle
选项:protected $middlewareGroups = [
'api' => [
'throttle:60,1',
'bindings',
],
];
为了将请求限制在每分钟60次,但对于某些路由(例如: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']);
});
但它不起作用。
有什么想法吗?
谢谢。
更新:
我注意到在api.php
路由中使用的'throttle:500,1'
会在Kernel.php
文件中指定的默认'throttle:60,1'
之后设置;因此,它无效。
记录过程执行情况,第一个调用是:
Illuminate\Routing\Middleware\ThrottleRequests -> handle
从 Kernel.php
中可以看出最大尝试次数为 60
。
接着,第二个调用是:
Illuminate\Routing\Middleware\ThrottleRequests -> handle
从api.php
的代码中可以看出,最大尝试次数为500
。
换句话说,api.php
文件中的throttle:500,1
不会覆盖Kernel.php
文件中的throttle:60,1
。
throttle:500,1
(更高的值),并将所有其他需要进行节流控制的路由设置为throttle:60,1
。这样可以按照请求顺序进行处理。 - manniL500
;然后我必须记住为每个新路由设置为60
。我更喜欢有一个标准值(例如:60
),并仅覆盖特定路由到500
。 - vlauciani