Laravel 5.4 - Throttle Override API: 60.1 '

I write a lot of APIs for retrieving and storing data.
I like the default throttle option:

 protected $middlewareGroups = [ 'api' => [ 'throttle:60,1', 'bindings', ], ]; 

to limit the request to 60 per minute; but for some route (es: POST ) I would like to increase this value.

I tried installing 'throttle:500,1' on the route middleware, as shown below:

 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']); }); 

but that will not work.

Any idea?

Thanks.

UPDATE:
I noticed that the 'throttle:500,1' used in the api.php route will be installed AFTER the default 'throttle:60,1' specified in the Kernel.php file; then it does not work.

When registering the execution of a process, the first call:

 Illuminate\Routing\Middleware\ThrottleRequests -> handle 

from Kernel.php has maxAttempts=60 .

Then the second call:

 Illuminate\Routing\Middleware\ThrottleRequests -> handle 

from api.php has maxAttempts=500 .

In other words, throttle:500,1 in the throttle:500,1 file api.php not override throttle:60,1 in the Kernel.php file.

+7
php throttle
source share
1 answer

Current answer

According to this GitHub problem , throttle middleware should not be used β€œtwice” (for example, you want to do this). There are only two ways to properly solve your current problem:

  • Write your own throttling middleware

or

  1. Define throttle middleware separately for each route (group)

Old answer

You have incorrectly installed the middleware key! When declaring multiple middlewares to create a new array for them

 ['middleware' => ['WriteToDatabaseMiddleware','throttle:500,1']] 

EDIT: Due to the middleware order, you have to set the kernel throttle to the highest value you want to use, and all other routes that should have a lower throttle value for the corresponding ones.

+6
source share

All Articles