PHPackages                             ggedde/spry-rate-limits - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. ggedde/spry-rate-limits

ActiveSpry-provider

ggedde/spry-rate-limits
=======================

Rate Limit Provider for Spry

1.0.9(4y ago)0261MITPHPPHP &gt;=5.4.0

Since May 23Pushed 4y ago1 watchersCompare

[ Source](https://github.com/ggedde/spry-rate-limits)[ Packagist](https://packagist.org/packages/ggedde/spry-rate-limits)[ RSS](/packages/ggedde-spry-rate-limits/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (11)Used By (1)

Spry Rate Limits
================

[](#spry-rate-limits)

This is a Spry Provider for adding Rate Limits to your Routes or a global Rate Limit for all requests

Installation
------------

[](#installation)

```
composer require ggedde/spry-rate-limits

```

### Activation

[](#activation)

In order to activate Rate Limits you need to initialze the Provider within your config and set the Rate Limit settings within your Config.

### Spry Configuration Example

[](#spry-configuration-example)

```
$config->rateLimits = [
  'driver' => 'file',
  'fileDirectory' =>  __DIR__.'/rate_limits',
];

Spry::addHook('initialized', 'Spry\\SpryProvider\\SpryRateLimits::initiate');
```

\* *By Default Rate Limits are not active, but you can set a global rate limit by adding the `default` settings to your Spry Config or by adding limits individually to each route.*

### Add a Global Rate Limit

[](#add-a-global-rate-limit)

```
$config->rateLimits = [
  'driver' => 'file',
  'fileDirectory' => __DIR__.'/rate_limits',
  'excludeTests' => false,
  'default' => [
      'by' => 'ip',
      'limit' => 10,
      'within' => 1,
      'hook' => 'configure',
      'excludeTests' => false
  ]
];
```

### Adding Limits per route

[](#adding-limits-per-route)

```
$config->routes = [
    '/auth/login' => [
        'label' => 'Auth Login',
        'controller' => 'Auth::login',
        'access' => 'public',
        'methods' => 'POST',
        'limits' => [
            'by' => 'ip',
            'limit' => 1,
            'within' => 3,
            'excludeTests' => false
        ],
        'params' => [
            'email' => [
                'required' => true,
                'type' => 'string',
            ],
            'password' => [
                'required' => true,
                'type' => 'string',
            ],
        ],
    ],
];
```

### Global Settings

[](#global-settings)

SettingTypeDefaultDescriptiondriverString''Driver to use to store the Rate Limit History. Currently only `db` and `file` is allowed. `db` uses SpryDB Provider to store the data so you must have SpryDB configured. In the future we plan to add `memcached` and `redis`. When setting the Driver to `db` you will need to make sure the table exists or you can run `spry migrate` to add the database automatically.dbTableString''When Driver is set to `db` you must set a Table to store the rate limit history.dbMetaArray\[\]When Driver is set to `db` you can pass meta data to the DB Provider.fileDirectoryString''When Driver is set to `file` you will need to pass a directory to store the files used to track the rate limits.defaultArray\[\]Default Global Rate Limit settings.excludeTestsBooleanfalseWhether to Exclude Tests when checking rate limits. This can be overwritten per route.### Rate Limit Settings

[](#rate-limit-settings)

SettingTypeDefaultDescriptionbyString'ip'Key used to identify the request. By default SpryRateLimits only supports `ip`. However, you can hook into this field and filter it with your own value. ex. `account_id` or `user_id`. See below for more details.limitNumber0Number of allowed requestswithinNumber0Time in Seconds to allow.excludeTestsBooleanfalseWhether to Exclude Tests when checking rate limits. If this is not set then the Global excludeTests setting will be applied.hookString'setRoute'When to run this Rate limiit. This uses Spry Hooks See ([Spry Lifecycles](https://github.com/ggedde/spry/blob/master/README.md#Lifecycle))### Adding your own Rate Limit (by) Key

[](#adding-your-own-rate-limit-by-key)

The default `by` key is `ip`, but many times this is not the best case. So you can add your own keys and values and filter the rate limit to change the value being checked.

```
Spry::addFilter('spryRateLimitKeys', function($keys){
  $keys['my_key'] = 'some_unique_value';
  return $keys;
});
```

Example retriving a value from Srpy's getAuth() method.

```
Spry::addHook('setAuth', function($auth){
    Spry::addFilter('spryRateLimitKeys', function($keys) use ($auth){
        $keys['user_id'] = $auth->user_id;
        $keys['account_id'] = $auth->account_id;
        return $keys;
    });
});
```

Extended Component Example

```
public static function setup()
{
    Spry::addHook('setAuth', function($auth){
        Spry::addFilter('spryRateLimitKeys', [__CLASS__, 'myMethod'], $auth);
    });
}

public static myMethod($keys, $meta, $auth)
{
    $keys['user_id'] = $auth->user_id;
    $keys['account_id'] = $auth->account_id;
    return $keys;
}
```

Using your new key in your Route

```
$config->routes = [
    '/data/get' => [
        'label' => 'Get Data',
        'controller' => 'SomeComponent::get',
        'access' => 'public',
        'methods' => 'GET',
        'limits' => [
            'limit' => 15,
            'within' => 15,
            'by' => 'user_id'
        ],
        'params' => [
            'id' => [
                'type' => 'string',
            ],
        ],
    ],
    '/users/get' => [
        'label' => 'Get User',
        'controller' => 'SomeUserComponent::get',
        'access' => 'public',
        'methods' => 'GET',
        'limits' => [
            'limit' => 1,
            'within' => 1,
            'by' => 'account_id'
        ],
        'params' => [
            'id' => [
                'type' => 'string',
            ],
        ],
    ],
];
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~78 days

Recently: every ~0 days

Total

10

Last Release

1475d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/459f086623b58e334ff8f569a9cedaf847ff4fef97b1c9aceabb618f9e2006fa?d=identicon)[ggedde](/maintainers/ggedde)

---

Top Contributors

[![ggedde](https://avatars.githubusercontent.com/u/3236909?v=4)](https://github.com/ggedde "ggedde (17 commits)")

---

Tags

rate-limitingspry

### Embed Badge

![Health badge](/badges/ggedde-spry-rate-limits/health.svg)

```
[![Health](https://phpackages.com/badges/ggedde-spry-rate-limits/health.svg)](https://phpackages.com/packages/ggedde-spry-rate-limits)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
