PHP code example of leuchtdiode / mezzio-fence

1. Go to this page and download the library: Download leuchtdiode/mezzio-fence 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/ */

    

leuchtdiode / mezzio-fence example snippets


use Try2catch\Fence\Fence;

#[Fence(limit: 5, perSeconds: 3600)]
class Start extends BaseJsonAction
{
}

use Try2catch\Fence\ConfigProvider as FenceConfigProvider;

$aggregator = new ConfigAggregator([
    // ...
    FenceConfigProvider::class,
]);

$app->pipe(RouteMiddleware::class);
$app->pipe(ImplicitHeadMiddleware::class);
$app->pipe(ImplicitOptionsMiddleware::class);
$app->pipe(MethodNotAllowedMiddleware::class);

$app->pipe(FenceMiddleware::class);

'fence' => [
    'enabled'           => true,
    'directory'         => 'data/fence',
    'default'           => [
        'limit'      => 300,
        'perSeconds' => 60,
    ],
    'trustedIps'        => [],
    'responder'         => Try2catch\Fence\Refusal\JsonResponder::class,
    'message'           => 'Too many requests',
    'pruneAfterSeconds' => 86400,
],

#[Fence(limit: 5)]                     // five a minute, if the default window is a minute
#[Fence(limit: 5, perSeconds: 3600)]   // five an hour
#[Fence(enabled: false)]               // never counted
json
{
    "success": false,
    "data": null,
    "meta": null,
    "errors": [
        {
            "code": "TOO_MANY_REQUESTS",
            "message": "Too many requests"
        }
    ]
}
bash
php vendor/bin/mezzio-console fence:prune