PHP code example of ryudith / mezzio-simple-throttle

1. Go to this page and download the library: Download ryudith/mezzio-simple-throttle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ryudith / mezzio-simple-throttle example snippets



...

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

...



...

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

    ...

};

...



...

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

...



...

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

...