aporat/laravel-rate-limiter
Composer 安装命令:
composer require aporat/laravel-rate-limiter
包简介
A flexible rate limiting middleware for Laravel and Lumen applications
README 文档
README
A flexible rate limiting middleware for Laravel applications, designed to throttle requests and actions using Redis.
Features
- Configurable rate limits per hour, minute, and second.
- Flexible limiting by IP, user ID, request method, and custom tags.
- IP blocking for abuse prevention.
- Optional rate limit headers in responses.
- Redis-backed storage for scalability.
Requirements
- PHP: 8.4 or higher
- Laravel: 12.x or 13.x
- Redis: Required for storage (ext-redis extension)
- Composer: Required for installation
Installation
Install the package via Composer:
composer require aporat/laravel-rate-limiter
The service provider (RateLimiterServiceProvider) is automatically registered via Laravel’s package discovery. If auto-discovery is disabled, add it to config/app.php:
'providers' => [ // ... Aporat\RateLimiter\Laravel\RateLimiterServiceProvider::class, ],
Optionally, register the facade for cleaner syntax:
'aliases' => [ // ... 'RateLimiter' => \Aporat\RateLimiter\Facades\RateLimiter::class, ],
Publish the configuration file:
php artisan vendor:publish --provider="Aporat\RateLimiter\Laravel\RateLimiterServiceProvider" --tag="config"
This copies rate-limiter.php to your config/ directory.
Configuration
Edit config/rate-limiter.php to adjust limits and Redis settings:
return [ 'limits' => [ 'hourly' => 3000, 'minute' => 60, 'second' => 10, ], 'log_errors' => true, // Set to false to disable logging of rate limit violations 'redis' => [ 'host' => env('RATE_LIMITER_REDIS_HOST', '127.0.0.1'), 'port' => env('RATE_LIMITER_REDIS_PORT', 6379), 'database' => env('RATE_LIMITER_REDIS_DB', 0), 'prefix' => env('RATE_LIMITER_REDIS_PREFIX', 'rate-limiter:'), ], ];
Add these to your .env file if needed:
RATE_LIMITER_REDIS_HOST=127.0.0.1
RATE_LIMITER_REDIS_PORT=6379
RATE_LIMITER_REDIS_DB=0
RATE_LIMITER_REDIS_PREFIX=rate-limiter:
Usage
Middleware
Apply rate limiting globally by registering the middleware in app/Http/Kernel.php:
protected $middleware = [ // ... \Aporat\RateLimiter\Laravel\Middleware\RateLimit::class, ];
Or apply it to specific routes:
Route::get('/api/test', function () { return 'Hello World'; })->middleware('Aporat\RateLimiter\Laravel\Middleware\RateLimit');
The middleware uses the configured limits (hourly, minute, second) and exempts IPs starting with 10.0..
Manual Rate Limiting
Use the RateLimiter facade for custom limiting:
use Aporat\RateLimiter\Laravel\Facades\RateLimiter; Route::post('/submit', function (Request $request) { RateLimiter::create($request) ->withUserId(auth()->id() ?? 'guest') ->withName('form_submission') ->withTimeInterval(3600) ->limit(5); // 5 submissions per hour return 'Submitted!'; });
IP Blocking
Block an IP manually:
RateLimiter::blockIpAddress('192.168.1.1', 86400); // Block for 24 hours
Check if an IP is blocked:
if (RateLimiter::isIpAddressBlocked()) { abort(403, 'Your IP is blocked.'); }
Rate Limit Headers
Add headers to responses:
$response = new Response('OK'); RateLimiter::create($request) ->withResponse($response) ->withRateLimitHeaders() ->limit(100); return $response; // Includes X-Rate-Limit-Limit and X-Rate-Limit-Remaining
Testing
Run the test suite:
composer test
Generate coverage reports:
composer test-coverage
Contributing
Contributions are welcome! Please:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/new-feature). - Commit your changes (
git commit -m "Add new feature"). - Push to the branch (
git push origin feature/new-feature). - Open a pull request.
Report issues at GitHub Issues.
License
This package is licensed under the MIT License. See the License File for details.
Support
- Issues: GitHub Issues
- Source: GitHub Repository
aporat/laravel-rate-limiter 适用场景与选型建议
aporat/laravel-rate-limiter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.93k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2020 年 08 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「throttle」 「middleware」 「laravel」 「lumen」 「rate-limiting」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aporat/laravel-rate-limiter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aporat/laravel-rate-limiter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aporat/laravel-rate-limiter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Microservice RPC through message queues.
The CodeIgniter Redis package
贝嘟分布式缓存扩展
A Laravel Pulse card to track users & URL's that are being throttled by your Rate Limiters
hyperf cache counter rate limiter
统计信息
- 总下载量: 1.93k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-22