PHP code example of razonyang / yii2-rate-limiter

1. Go to this page and download the library: Download razonyang/yii2-rate-limiter 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/ */

    

razonyang / yii2-rate-limiter example snippets


return [
    public function behaviors()
    {
        return [
            // redis via redis extension
            'rateLimiter' => [
                'class' => \RazonYang\Yii2\RateLimiter\RedisRateLimiter::class,
                'password' => '',
                'hostname' => 'localhost',
                'port' => 6379,
                'capacity' => 5000,
                'rate' => 0.72,
                'limitPeriod' => 3600,
                'prefix' => 'rate_limiter:',
                'ttl' => 3600,
                // 'nameCallback' => $callback,
            ],
            // redis via yii2-redis
            'rateLimiter' => [
                'class' => \RazonYang\Yii2\RateLimiter\Redis\RateLimiter::class,
                'redis' => 'redis', // redis component name or definition
                'capacity' => 5000,
                'rate' => 0.72,
                'limitPeriod' => 3600,
                'prefix' => 'rate_limiter:',
                'ttl' => 3600,
                // 'nameCallback' => $callback,
            ],

            // memcached
            'rateLimiter' => [
                'class' => \RazonYang\Yii2\RateLimiter\MemcachedRateLimiter::class,
                'hostname' => 'localhost',
                'port' => 11211,
                'capacity' => 5000,
                'rate' => 0.72,
                'limitPeriod' => 3600,
                'prefix' => 'rate_limiter:',
                'ttl' => 3600,
                // 'nameCallback' => $callback,
            ],
        ];
    }
];

$nameCallback = function (
    \yii\web\User $user,
    \yii\web\Request $request,
    \yii\base\Action $action
): string {
    return 'bucket name';
}