The fet/laminas-rate-limiting
package provides a rate limiting solution for Laminas MVC applications. It allows you to set limits on how often certain routes can be accessed within a specified time window.
To install this package, use Composer:
composer require fet/laminas-rate-limiting
Make sure you have Composer installed on your system before running this command.
Enable the Fet\LaminasRateLimiting
module in your config/application.config.php
file.
return [
// ... other modules ...
'Fet\\LaminasRateLimiting',
];
Define your rate limiting rules in your module.config.php
or global configuration file.
return [
'rate_limiting' => [
'enabled' => true, // indicates whether rate limiting is enabled
'max_requests' => 100, // max number of requests
'window' => 60, // time window in seconds
'routes' => [ // routes to apply rate limiting (wildcards allowed)
'home',
'api/*',
'admin/*',
],
],
];
If you want to limit all routes, you can use a '*' as route name.
As of the current release, this package exclusively supports Redis for backend storage.
return [
'rate_limiting' => [
// ... other config options ...
'storage' => [
'options' => [
'host' => '127.0.0.1',
'port' => 6379
],
],
],
];
If you want to provide your own storage implementation, make sure your class adheres to the Fet\LaminasRateLimiting\Storage\StorageInterface
contract.
use Acme\Storage\DatabaseStorage;
return [
'rate_limiting' => [
// ... other config options ...
'storage' => [
'name' => DatabaseStorage::class, // this will be loaded via service manager
'options' => [],
],
],
];
Run the tests with:
composer test