Skip to content

AcidF0x/Laravel-EzThrottle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel-EzThrottle

A simple Laravel Throttle extension

Installation

install using composer:

 composer require acidf0x/laravel-ez-throttle

Basic Usage

Start by creating an Throttle instance

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();   
    }
    
}

or use the EzThrottle trait if you want

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();
            }
        }
        
        //......
    }
}

Customize

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

Localization

# 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

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

Todos

  • change Trait Method
  • Test and support Variable Laravel Ver. (5.*)
  • Change Composer Dependency