PHP code example of osid / laravel-api-rate-limiter

1. Go to this page and download the library: Download osid/laravel-api-rate-limiter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

osid / laravel-api-rate-limiter example snippets


return [
    'global' => [
        'limit' => 1000, // Requests per hour
        'burst' => 50, // Burst limit
    ],
    'routes' => [
        'api/v1/users' => [
            'limit' => 500,
            'burst' => 20,
        ],
        'api/v1/orders' => [
            'limit' => 200,
            'burst' => 10,
        ],
    ],
    'user_based' => true, // Enable user-specific limits
    'ip_based' => true, // Enable IP-based limits
    'cache_driver' => 'redis', // Cache driver to use
    'response_messages' => [
        'rate_limit_exceeded' => 'Too many requests. Please try again later.',
    ],
];

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
use App\Http\Controllers\OrderController;

Route::middleware(['api.rate.limiter'])->group(function () {
    Route::get('/users', [UserController::class, 'index']);
    Route::get('/orders', [OrderController::class, 'index']);
    // Other routes
});

'api/v1/users' => [
    'limit' => 500,
    'burst' => 20,
],
'api/v1/orders' => [
    'limit' => 200,
    'burst' => 10,
],

'response_messages' => [
    'rate_limit_exceeded' => 'Too many requests. Please try again later.',
],
bash
   php artisan vendor:publish --provider="osid\ApiRateLimiter\ApiRateLimiterServiceProvider" --tag=config