PHPackages                             wolfcode/rate-limiting - 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. wolfcode/rate-limiting

ActiveLibrary

wolfcode/rate-limiting
======================

php rate-limiting

v0.1.3(12mo ago)03.5k—0%1[1 issues](https://github.com/wolf-leo/rate-limiting/issues)Apache-2.0PHPPHP &gt;=8.1

Since Mar 4Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/wolf-leo/rate-limiting)[ Packagist](https://packagist.org/packages/wolfcode/rate-limiting)[ RSS](/packages/wolfcode-rate-limiting/feed)WikiDiscussions main Synced today

READMEChangelog (4)DependenciesVersions (5)Used By (0)

PHP rate-limiting
=================

[](#php-rate-limiting)

1. Require
----------

[](#1-require)

> php &gt;= 8.1
>
> Redis

2. Install
----------

[](#2-install)

```
composer require "wolfcode/rate-limiting"
```

3. Usage
--------

[](#3-usage)

```
use Wolfcode\RateLimiting\Attributes\RateLimitingMiddleware;

class Test
{
    // 每1秒只能请求1次
    // Only one request can be made per second
    #[RateLimitingMiddleware(key: 'test', seconds: 1, limit: 1, message: '请求过于频繁~')]
    public function index(Request $request): string

    // 每60秒只能请求100次
    // Only 100 requests can be made every 60 seconds
    #[RateLimitingMiddleware(key: 'test', seconds: 60, limit: 100, message: '你好快啊，我好喜欢~')]
    public function index(Request $request): string

    // 每3秒只能请求10次 key可以使用数组回调方式 参考下方例子
    // Only 10 key requests can be made every 3 seconds. An array callback method can be used, as shown in the example below
    #[RateLimitingMiddleware(key: [Some::class,'getIp'], seconds: 3, limit: 10, message: '我记住你了~')]
    public function index(Request $request): string

    // 每3秒只能请求10次 key可以使用数组回调方式 参考下方例子
    // Only 10 key requests can be made every 3 seconds. An array callback method can be used, as shown in the example below
    #[RateLimitingMiddleware(key: [Some::class,'customIp'], seconds: 3, limit: 10, message: '我记住你了~'),args:[__METHOD__])]
    public function index(Request $request): string
}

// 需要自行创建一个 Some 类 并且存在静态方法 getIp
// Need to create a Some class on your own and have a static method getIp
class Some
{
    public static function getIp(): string
    {
        return $request->ip();
    }

    public static function customIp(...$args): string
    {
        return $args[0] . $request->ip();
    }
}
```

4. Suggestion
-------------

[](#4-suggestion)

> 可以在 .env 文件中设置一个 RATE\_LIMITING\_STATUS 开关，来控制是否开启限流
>
> 建议在中间件中使用
>
> You can set a RATE\_LIMITING-STATUS switch in the .env file to control whether to enable current limiting
>
> Suggest using it in middleware

5. Example
----------

[](#5-example)

#### ThinkPHP8.1

[](#thinkphp81)

```
