A Laravel package to manage rate limiters for APIs, used internally at Oltrematica for our projects.
This package provides a simple configuration for rate limiters in APIs, allowing easy management of requests for login, registration, and general API routes. It is designed to be straightforward to integrate and configure within Laravel projects.
To install the package, run the following command in your terminal:
composer require oltrematica/laravel-rate-limiter
The package includes a configuration file that can be customized. To publish the configuration file to your project, run the following command:
php artisan vendor:publish --provider="Oltrematica\RateLimiter\RateLimiterServiceProvider" --tag="ratelimiter-config"
This will publish the rate-limiter.php
configuration file into your project's config
directory.
To apply rate limiters, use the following middleware in your routes:
- API Rate Limiter: Use the
throttle:api
middleware to apply rate limiting to API requests. - Login Rate Limiter: Use the
throttle:login
middleware to apply rate limiting to login requests. - Register Rate Limiter: Use the
throttle:register
middleware to apply rate limiting to registration requests.
In routes/api.php
:
Route::middleware(['throttle:api'])->group(function () {
// API routes go here
});
In routes/web.php
:
Route::post('/login', 'LoginController@login')->middleware('throttle:login');
In routes/web.php
:
Route::post('/register', 'RegisterController@register')->middleware('throttle:register');
The rate-limiter.php
configuration file allows you to customize request limits for each type of rate
limiter. You can override default values using environment variables in your .env
file.
RATE_LIMITING_API_IGNORE_ADMINS=true
RATE_LIMITING_API_LIMIT=60
RATE_LIMITING_API_LOGIN_LIMIT=60
RATE_LIMITING_API_LOGIN_LIMIT_PER_EMAIL=10
RATE_LIMITING_API_REGISTER_LIMIT=60
The project includes automated tests and tools for code quality control.
Rector is a tool for automating code refactoring and migrations. It can be run using the following command:
composer refactor
PhpStan is a tool for static analysis of PHP code. It can be run using the following command:
composer analyse
Pint is a tool for formatting PHP code. It can be run using the following command:
composer format
The project includes automated tests and tools for code quality control.
composer test
Feel free to contribute to this package by submitting issues or pull requests. We welcome any improvements or bug fixes you may have.