fet/laminas-rate-limiting

A rate limiting module for Laminas MVC applications.

0.3.0 2024-02-01 11:39 UTC

This package is auto-updated.

Last update: 2024-05-11 13:24:18 UTC


README

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.

Installation

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',
];

Configuration

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.

Storages

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
            ],
        ],
    ],
];

Custom storage

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' => [],
        ],
    ],
];

Tests

Run the tests with:

composer test