acidf0x/laravel-ez-throttle

一个简单的 Laravel 限流扩展

v1.0.0 2018-01-11 03:04 UTC

This package is not auto-updated.

Last update: 2024-09-21 16:59:09 UTC


README

一个简单的 Laravel 限流扩展

安装

使用 composer 安装

 composer require acidf0x/laravel-ez-throttle

基本用法

首先创建一个 Throttle 实例

use AcidF0x\EzThrottle\Throttle;  

$throttle = new Throttle();
// or
$throttle = new Throttle($throttleKey , $maxAttempts, $decayMinutes);


// increase hit count
$throttle->hit()

if ($throttle->isBlock()) {
    echo $throttle->getErrorMsg(); // "Too Many Requests. Please try again in 1 minutes"
} else {
    // ...
    
    if ( ... ) {
        $throttle->clear();   
    }
    
}

或者如果你想要使用 EzThrottle 特性

use AcidF0x\EzThrottle\Foundation\EzThrottle;

class SomeController extends Controller
{
    use EzThrottle;
    
    $protected $ThrottleKey = 'LoginThrottle';
    $protected $maxAttempts = '3';
    $protected $decayMinutes = '1';
    
    public function doLogin()
    {
        //.....
        
        // increase hit count
        $this->hit()
        if ($this->isBlock()) {
            return $this->getErrorMsg(); // "Too Many Requests. Please try again in 1 minutes"
        } else {
            // ...
            if ( ... ) {
                $this->clear();
            }
        }
        
        //......
    }
}

自定义

php artisan vendor:publish --provider=AcixF0x\Ezthrrotle\EzthrottleServiceProvider

本地化

# resources/lang/vendor/ezthrottle/en/error.php

return [
        'sec'=> 'Too Many Requests. Please try again in :sec seconds',
        'min'=> 'Too Many Requests. Please try again in :min minutes',
        'hour'=> 'Too Many Requests. Please try again in :hour hours',
        'days'=> 'Too Many Requests. Please try again in :day days',
];

配置

# config/ezthrottle.php
return [
    'defaultThrottleKey' => 'throttle', 
    'defaultDecayMinutes' => '1',
    'defaultMaxAttempts' => '3'
];

待办事项

  • 更改特性方法
  • 测试并支持可变 Laravel 版本 (5.*)
  • 更改 Composer 依赖