rogervila / lumen-rate-limiting by rogervila

Lumen port of Laravel ThrottleRequests middleware
28,922
15
3
Package Data
Maintainer Username: rogervila
Maintainer Contact: rogervila@me.com (Roger Vilà)
Package Create Date: 2020-11-10
Package Last Update: 2023-04-18
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-19 15:16:16
Package Statistics
Total Downloads: 28,922
Monthly Downloads: 1,143
Daily Downloads: 48
Total Stars: 15
Total Watchers: 3
Total Forks: 2
Total Open Issues: 1

About

This package contains Lumen port of Laravel's ThrottleRequests middleware

Install

  1. Require the package on your Lumen application
composer require rogervila/lumen-rate-limiting
  1. Make sure that AppServiceProvider and AuthServiceProvider are uncommented on bootstrap/app.php
$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class);
  1. Configure a rate limiter on the AppServiceProvider boot method
/**
 * Configure global rate limiter
 *
 * @return void
 */
public function boot()
{
    app(\Illuminate\Cache\RateLimiter::class)->for('global', function () {
        return \Illuminate\Cache\RateLimiting\Limit::perMinute(60)->by(request()->ip());
    });
}
  1. Register the middleware on bootstrap/app.php
$app->routeMiddleware([
    'throttle' => \LumenRateLimiting\ThrottleRequests::class,
]);
  1. Add the middleware to the global router group on bootstrap/app.php
$app->router->group([
    'namespace' => 'App\Http\Controllers',
    'middleware' => 'throttle:global',
], function ($router) {
    require __DIR__ . '/../routes/web.php';
});

The middleware can be placed on specific routes instead of globally, as defined on the official documentation.

License

This project is open-sourced software licensed under the MIT license.