Package Data | |
---|---|
Maintainer Username: | amiiiiink |
Maintainer Contact: | karimi66.amin@gmail.com (Amin Karimi) |
Package Create Date: | 2025-08-04 |
Package Last Update: | 2025-08-11 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2025-08-19 15:00:04 |
Package Statistics | |
---|---|
Total Downloads: | 4 |
Monthly Downloads: | 4 |
Daily Downloads: | 0 |
Total Stars: | 1 |
Total Watchers: | 0 |
Total Forks: | 0 |
Total Open Issues: | 1 |
A flexible rate limiting middleware for Laravel applications, designed to throttle requests and actions using Redis.
Install the package via Composer:
composer require amiiiiink/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
:
Publish the configuration file:
php artisan vendor:publish --tag=config
This copies rate-limiter.php
to your config/
directory.
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:
Apply rate limiting globally by registering the middleware in app/Http/Kernel.php
:
protected $middleware = [
// ...
\amiiiiink\RateLimiter\Laravel\Middleware\RateLimit::class,
];
Or apply it to specific routes:
Route::get('/api/test', function () {
return 'Hello World';
})->middleware('amiiiiink\RateLimiter\Laravel\Middleware\RateLimit');
The middleware uses the configured limits (hourly
, minute
, second
) and exempts IPs starting with 10.0.
.
Use the RateLimiter
facade for custom limiting:
use amiiiiink\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!';
});
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.');
}
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
Run the test suite:
composer test
Generate coverage reports:
composer test-coverage
Contributions are welcome! Please:
git checkout -b feature/new-feature
).git commit -m "Add new feature"
).git push origin feature/new-feature
).Report issues at GitHub Issues.
This package is licensed under the MIT License. See the License File for details.