Skip to content

Commit 99cf56b

Browse files
[9.x] Add original rate limiter in laravel (#8549)
* add original rate limiter in laravel * Update routing.md * Update routing.md --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 647886b commit 99cf56b

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

routing.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,23 @@ Using the `Route::fallback` method, you may define a route that will be executed
671671
<a name="defining-rate-limiters"></a>
672672
### Defining Rate Limiters
673673

674-
Laravel includes powerful and customizable rate limiting services that you may utilize to restrict the amount of traffic for a given route or group of routes. To get started, you should define rate limiter configurations that meet your application's needs. Typically, this should be done within the `configureRateLimiting` method of your application's `App\Providers\RouteServiceProvider` class.
674+
Laravel includes powerful and customizable rate limiting services that you may utilize to restrict the amount of traffic for a given route or group of routes. To get started, you should define rate limiter configurations that meet your application's needs. Typically, this should be done within the `configureRateLimiting` method of your application's `App\Providers\RouteServiceProvider` class, which already includes a rate limiter definition that is applied to the routes in your application's `routes/api.php` file:
675+
676+
```php
677+
use Illuminate\Cache\RateLimiting\Limit;
678+
use Illuminate\Http\Request;
679+
use Illuminate\Support\Facades\RateLimiter;
680+
681+
/**
682+
* Configure the rate limiters for the application.
683+
*/
684+
protected function configureRateLimiting(): void
685+
{
686+
RateLimiter::for('api', function (Request $request) {
687+
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
688+
});
689+
}
690+
```
675691

676692
Rate limiters are defined using the `RateLimiter` facade's `for` method. The `for` method accepts a rate limiter name and a closure that returns the limit configuration that should apply to routes that are assigned to the rate limiter. Limit configuration are instances of the `Illuminate\Cache\RateLimiting\Limit` class. This class contains helpful "builder" methods so that you can quickly define your limit. The rate limiter name may be any string you wish:
677693

0 commit comments

Comments
 (0)