![](/img/trans.png)
[英]Laravel API Routing Not Doing Throttling - Always Set To Default 60,1
[英]Laravel 5.4 - Override API 'throttle:60,1'
我正在编写很多 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']);
});
但它不起作用。
任何的想法?
谢谢。
更新:
我注意到api.php
路由中使用的'throttle:500,1'
将在Kernel.php
文件中指定的默认'throttle:60,1'
Kernel.php
'throttle:60,1'
之后设置; 然后,它不起作用。
记录流程执行,第一个调用是:
Illuminate\Routing\Middleware\ThrottleRequests -> handle
从Kernel.php
有maxAttempts=60
。
然后,第二个调用是:
Illuminate\Routing\Middleware\ThrottleRequests -> handle
来自api.php
maxAttempts=500
。
换句话说, api.php
文件中的throttle:500,1
不会覆盖api.php
文件中的throttle:60,1
Kernel.php
。
根据这个 GitHub 问题,油门中间件不应该“两次”使用(就像你想那样做)。 只有两种方法可以“正确”处理您当前的问题:
或
您设置的中间件密钥错误! 声明多个要使用的中间件时,为它们创建一个新数组
['middleware' => ['WriteToDatabaseMiddleware','throttle:500,1']]
编辑:由于中间件顺序,您应该将内核节流阀设置为您想要使用的最高值,并将所有其他路由的节流阀值设置为相应的较低值。
在 Laravel 6 中,您可以使用前缀来防止全局节流。 使用'throttle:5,1,prefix'
Route::group(['prefix' => 'contact-us', 'middleware' => 'throttle:5,1,contact-form',], function () {
Route::post('/', 'ContactUsController@store');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.