Ratelimiter
Ratelimiter
Class Ratelimiter.
class Ratelimiter implements \Hazaar\Middleware\Interface\MiddlewareImplements: Hazaar\Middleware\Interface\Middleware
Middleware implementation for rate limiting incoming requests. This class is intended to be used within the Hazaar Framework to control the rate at which clients can access certain routes or controllers.
Implements the Middleware interface.
Properties
rateLimiter
public UtilRateLimiter $rateLimiterType: Hazaar\Util\RateLimiter
requests
the maximum number of allowed requests within the specified time window
private int $requestsType: int
seconds
the duration of the time window in seconds
private int $secondsType: int
backend
the backend storage mechanism to use for rate limiting (default is 'cache')
private string $backend = 'cache'Type: string
bucket
the name of the budget this limit counts against, or a closure that returns it. Clients get a separate count per bucket. Defaults to the request path.
private \Closure|string|null $bucketType: Closure|string|null
Methods
__construct
Constructs a new rate limiter middleware instance.
public __construct(int $requests, int $seconds, string $backend = 'cache', \Closure|string|null $bucket): voidThe limiter and its backend aren't created until the first request, so a backend that isn't available only fails the rate-limited requests, not route loading.
Parameters
| Parameter | Type | Description |
|---|---|---|
$requests | int | the maximum number of allowed requests within the specified time window |
$seconds | int | the duration of the time window in seconds |
$backend | string | the backend storage mechanism to use for rate limiting (default is 'cache') |
$bucket | Closure | string |
handle
Handles the incoming request and applies rate limiting.
public handle(Request $request, callable $next, mixed $args): ResponseReturns: Hazaar\Controller\Response
Parameters
| Parameter | Type | Description |
|---|---|---|
$request | Hazaar\Application\Request | the incoming HTTP request |
$next | callable | the next middleware or handler to call |
$args | mixed |
Generated by Hazaar API Doc Generator on Thu, 22 Oct 2026 05:05:29 +0000