ryudith / mezzio-simple-throttle
Simple throttle middleware for Mezzio.
Requires
- php: ^7.4 || ~8.0.0 || ~8.1.0
- psr/container: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.5.11
This package is auto-updated.
Last update: 2024-05-10 12:03:48 UTC
README
Ryudith\MezzioSimpleThrottle
is throttle middleware for Mezzio framework.
Instalation
To install run the following command :
$ composer require ryudith/mezzio-simple-throttle
Usage
First add ConfigProvider::class
to config/config.php
... $aggregator = new ConfigAggregator([ ... \Ryudith\MezzioSimpleThrottle\ConfigProvider::class, // <= add this line ... class_exists(\Mezzio\Swoole\ConfigProvider::class) ? \Mezzio\Swoole\ConfigProvider::class : function (): array { return []; }, ... ], $cacheConfig['config_cache_path']); ...
Then register middleware by add SimpleThrottle::class
to config/pipeline.php
... use Ryudith\MezzioSimpleThrottle\SimpleThrottle; // <= add this line ... return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container): void { // The error handler should be the first (most outer) middleware to catch // all Exceptions. $app->pipe(ErrorHandler::class); $app->pipe(ServerUrlMiddleware::class); $app->pipe(SimpleThrottle::class); // <= add this line ... }; ...
You can add
$app->pipe(SimpleThrottle::class)
before$app->pipe(ErrorHandler::class)
if you want.
Custom configuration
Configuration for middleware is locate in vendor/ryudith/mezzio-simple-throttle/ConfigProvider.php
which the important content is :
... return [ 'dependencies' => [ 'factories' => [ FileSystemThrottleStorage::class => FileSystemThrottleStorageFactory::class, ThrottleResponse::class => ThrottleResponseFactory::class, SimpleThrottle::class => SimpleThrottleFactory::class, ], ], 'mezzio_simple_throttle' => [ 'request_limit_per_minute' => 10, 'request_real_ip_key' => 'REMOTE_ADDR', // key for $_ENV or $_SERVER to get request real ip 'ip_path_key' => true, // data key based IP and URI path or IP only data key 'throttle_data_dir' => './data/throttle', 'file_data_delimiter' => '||', 'throttle_storage_class' => FileSystemThrottleStorage::class, 'throttle_response_class' => ThrottleResponse::class, ], ]; ...
Detail :
-
request_limit_per_minute
is how many hit before trigger throttle -
request_real_ip_key
is assoc key to get request IP, default is 'REMOTE_ADDR' which you can change if you have custom key on your webserver. -
ip_path_key
is flag to generate key if use IP-Path combine or just IP address. -
throttle_data_dir
is string path location to save throttle record data. -
file_data_delimiter
is data delimiter inside file, since this library file based record data. -
throttle_storage_class
is service key to save throttle data. You can change if you want another storage type (do not forget implement interfaceRyudith\MezzioSimpleThrottle\Storage\StorageInterface
on your custom class) -
throttle_response_class
is class to give response when throttle hit limit. Also you can change this if you want use your own class (do not forget implement interfaceRyudith\MezzioSimpleThrottle\Response\ThrottleResponseInterface
on your own class).
You do not have to edit configuration inside ConfigProvider.php
directly to change configuration, just add configuration you want to change to config/autoload/mezzio.global.php
for example or any configuration file you use.
For example you can add your custom configuration like this inside config/autoload/mezzio.global.php
:
... return [ // Toggle the configuration cache. Set this to boolean false, or remove the // directive, to disable configuration caching. Toggling development mode // will also disable it by default; clear the configuration cache using // `composer clear-config-cache`. ConfigAggregator::ENABLE_CACHE => true, // Enable debugging; typically used to provide debugging information within templates. 'debug' => true, 'mezzio' => [ // Provide templates for the error handling middleware to use when // generating responses. 'error_handler' => [ 'template_404' => 'error::4042', 'template_error' => 'error::error', ], ], // add only configuration you want to change 'mezzio_simple_throttle' => [ 'ip_path_key' => false, 'file_data_delimiter' => '//' ], ]; ...
Documentation
Next
- Add exclude path and/or IP