2017 © Pedro Peláez
 

library throttle

Throttle Is A Rate Limiter For Laravel 5

image

graham-campbell/throttle

Throttle Is A Rate Limiter For Laravel 5

  • Sunday, March 4, 2018
  • by graham-campbell
  • Repository
  • 18 Watchers
  • 452 Stars
  • 531,664 Installations
  • PHP
  • 12 Dependents
  • 0 Suggesters
  • 49 Forks
  • 3 Open issues
  • 31 Versions
  • 4 % Grown

The README.md

Laravel Throttle

Laravel Throttle was created by, and is maintained by Graham Campbell, and is a rate limiter for Laravel. Feel free to check out the change log, releases, security policy, license, code of conduct, and contribution guidelines., (*1)

Banner, (*2)

Build Status StyleCI Status Software License Packagist Downloads Latest Version , (*3)

Installation

This version requires PHP 7.4-8.3 and supports Laravel 8-11., (*4)

Throttle L5.5 L5.6 L5.7 L5.8 L6 L7 L8 L9 L10 L11
7.5 :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark: :x: :x: :x: :x:
8.2 :x: :x: :x: :x: :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark: :x: :x:
9.0 :x: :x: :x: :x: :x: :x: :white_check_mark: :white_check_mark: :x: :x:
10.2 :x: :x: :x: :x: :x: :x: :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark:

To get the latest version, simply require the project using Composer:, (*5)

$ composer require "graham-campbell/throttle:^10.2"

Once installed, if you are not using automatic package discovery, then you need to register the GrahamCampbell\Throttle\ThrottleServiceProvider service provider in your config/app.php., (*6)

You can also optionally alias our facade:, (*7)

        'Throttle' => GrahamCampbell\Throttle\Facades\Throttle::class,

Configuration

Laravel Throttle supports optional configuration., (*8)

To get started, you'll need to publish all vendor assets:, (*9)

$ php artisan vendor:publish

This will create a config/throttle.php file in your app that you can modify to set your configuration. Also, make sure you check for changes to the original config file in this package between releases., (*10)

There is one config option:, (*11)

Cache Driver

This option ('driver') defines the cache driver to be used. It may be the name of any driver set in config/cache.php. Setting it to null will use the driver you have set as default in config/cache.php. The default value for this setting is null., (*12)

Usage

Throttle

This is the class of most interest. It is bound to the ioc container as 'throttle' and can be accessed using the Facades\Throttle facade. There are six public methods of interest., (*13)

The 'get' method will create a new throttler class (a class that implements Throttler\ThrottlerInterface) from the 1-3 parameters that you pass to it. The first parameter is required and must either an instance of \Illuminate\Http\Request, or an associative array with two keys ('ip' should be the ip address of the user you wish to throttle and 'route' should be the full url you wish to throttle, but actually, for advanced usage, may be any unique key you choose). The second parameter is optional and should be an int which represents the maximum number of hits that are allowed before the user hits the limit. The third and final parameter should be an int that represents the time the user must wait after going over the limit before the hit count will be reset to zero. Under the hood this method will be calling the make method on a throttler factory class (a class that implements Factories\FactoryInterface)., (*14)

The other 5 methods all accept the same parameters as the get method. What happens here is we dynamically create a throttler class (or we automatically reuse an instance we already created), and then we call the method on it with no parameters. These 5 methods are 'attempt', 'hit', 'clear', 'count', and 'check'. They are all documented bellow., (*15)

Facades\Throttle

This facade will dynamically pass static method calls to the 'throttle' object in the ioc container which by default is the Throttle class., (*16)

Throttler\ThrottlerInterface

This interface defines the public methods a throttler class must implement. All 5 methods here accept no parameters., (*17)

The 'attempt' method will hit the throttle (increment the hit count), and then will return a boolean representing whether or not the hit limit has been exceeded., (*18)

The 'hit' method will hit the throttle (increment the hit count), and then will return $this so you can make another method call if you so choose., (*19)

The 'clear' method will clear the throttle (set the hit count to zero), and then will return $this so you can make another method call if you so choose., (*20)

The 'count' method will return the number of hits to the throttle., (*21)

The 'check' method will return a boolean representing whether or not the hit limit has been exceeded., (*22)

Throttler\CacheThrottler

This class implements Throttler\ThrottlerInterface completely. This is the only throttler implementation shipped with this package, and in created by the Factories\CacheFactory class. Note that this class also implements PHP's Countable interface., (*23)

Factories\FactoryInterface

This interface defines the public methods a throttler factory class must implement. Such a class must only implement one method., (*24)

The 'make' method will create a new throttler class (a class that implements Throttler\ThrottlerInterface) from data object you pass to it. This documentation of an internal interface is included for advanced users who may wish to write their own factory classes to make their own custom throttler classes., (*25)

Factories\CacheFactory

This class implements Factories\FactoryInterface completely. This is the only throttler implementation shipped with this package, and is responsible for creating the Factories\CacheFactory class. This class is only intended for internal use by the Throttle class., (*26)

Http\Middleware\ThrottleMiddleware

You may put the GrahamCampbell\Throttle\Http\Middleware\ThrottleMiddleware middleware in front of your routes to throttle them. The middleware can take up to two parameters. The two parameters are limit and time. It may be useful for you to take a look at the source for this, read the tests, or check out Laravel's documentation if you need to., (*27)

ThrottleServiceProvider

This class contains no public methods of interest. This class should be added to the providers array in config/app.php. This class will setup ioc bindings., (*28)

Real Examples

Here you can see an example of just how simple this package is to use., (*29)

Our first example will be a super simple usage of our default middleware. This will setup a middleware for that url with a limit of 10 hits and a retention time of 1 hour., (*30)

use Illuminate\Support\Facades\Route;

Route::get('foo', ['middleware' => 'GrahamCampbell\Throttle\Http\Middleware\ThrottleMiddleware', function () {
    return 'Why herro there!';
}]);

What if we want custom limits? Easy! Laravel allows us to pass parameters to a middleware. This will setup a middleware for that url with a limit of 50 hits and a retention time of 30 mins., (*31)

use Illuminate\Support\Facades\Route;

Route::get('foo', ['middleware' => 'GrahamCampbell\Throttle\Http\Middleware\ThrottleMiddleware:50,30', function () {
    return 'Why herro there!';
}]);

What if we don't want to use the default middleware provided with this package? Well, that's easy too., (*32)

use GrahamCampbell\Throttle\Facades\Throttle;
use Illuminate\Support\Facades\Request;

// now let's get a throttler object for that request
// we'll use the same config as in the previous example
// note that only the first parameter is "required"
$throttler = Throttle::get(Request::instance(), 50, 30);

// let's check if we've gone over the limit
var_dump($throttler->check());

// we implement Countable
var_dump(count($throttler));

// there are a few more functions available
// please see the previous documentation

Also note that you can call methods straight on the factory instead of calling the get method., (*33)

use GrahamCampbell\Throttle\Facades\Throttle;
use Illuminate\Support\Facades\Request;

$request = Request::instance();

// the attempt function will hit the throttle, then return check
var_dump(Throttle::attempt($request));

// so this is the same as writing
var_dump(Throttle::hit($request)->check());

// and, of course, the same as
var_dump(Throttle::get($request)->attempt());
Further Information

There are other classes in this package that are not documented here (such as the transformers). This is because they are not intended for public use and are used internally by this package., (*34)

Security

If you discover a security vulnerability within this package, please send an email to security@tidelift.com. All security vulnerabilities will be promptly addressed. You may view our full security policy here., (*35)

License

Laravel Throttle is licensed under The MIT License (MIT)., (*36)

For Enterprise

Available as part of the Tidelift Subscription, (*37)

The maintainers of graham-campbell/throttle and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more., (*38)

The Versions

04/03 2018

dev-master

9999999-dev

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

04/03 2018

v7.0.1

7.0.1.0

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

01/03 2018

v7.0.0

7.0.0.0

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

02/01 2018

6.0.x-dev

6.0.9999999.9999999-dev

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

02/01 2018

v6.0.1

6.0.1.0

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

12/08 2017

dev-analysis-q1Ndyj

dev-analysis-q1Ndyj

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

06/08 2017

v6.0.0

6.0.0.0

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

02/06 2017

dev-analysis-qrEGJx

dev-analysis-qrEGJx

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

01/01 2017

v5.3.0

5.3.0.0

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

01/01 2017

5.3.x-dev

5.3.9999999.9999999-dev

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

26/04 2016

v5.2.0

5.2.0.0

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

30/01 2016

v5.1.1

5.1.1.0

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

14/11 2015

v5.1.0

5.1.0.0

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

06/10 2015

v5.0.0

5.0.0.0

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

03/08 2015

v4.1.1

4.1.1.0

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

03/08 2015

4.1.x-dev

4.1.9999999.9999999-dev

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

26/06 2015

v4.1.0

4.1.0.0

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

21/05 2015

v4.0.0

4.0.0.0

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

05/02 2015

v3.0.0

3.0.0.0

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

05/02 2015

3.0.x-dev

3.0.9999999.9999999-dev

Throttle Is A Rate Limiter For Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

05/01 2015

v2.0.2

2.0.2.0

Throttle Is A Rate Limiter For Laravel 4.1/4.2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

05/01 2015

2.0.x-dev

2.0.9999999.9999999-dev

Throttle Is A Rate Limiter For Laravel 4.1/4.2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

30/10 2014

v2.0.1

2.0.1.0

Throttle Is A Rate Limiter For Laravel 4.1/4.2

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

05/10 2014

v2.0.0

2.0.0.0

Throttle Is A Rate Limiter For Laravel 4.1/4.2

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

09/08 2014

v1.0.2

1.0.2.0

Throttle Is A Rate Limiter For Laravel 4.1+

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

09/08 2014

1.0.x-dev

1.0.9999999.9999999-dev

Throttle Is A Rate Limiter For Laravel 4.1+

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

02/08 2014

v1.0.1

1.0.1.0

Throttle Is A Rate Limiter For Laravel 4.1+

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

21/07 2014

v1.0.0

1.0.0.0

Throttle Is A Rate Limiter For Laravel 4.1+

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

16/07 2014

v1.0.0-RC1

1.0.0.0-RC1

Throttle Is A Rate Limiter For Laravel 4.1+

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

21/04 2014

v0.2.0-alpha

0.2.0.0-alpha

Throttle Is A Rate Limiter For Laravel 4.1

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle

08/02 2014

v0.1.0-alpha

0.1.0.0-alpha

Throttle Is A Rate Limiter For Laravel 4.1

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Graham Campbell

laravel framework graham campbell grahamcampbell throttle rate limit throttling laravel throttle laravel-throttle