Handler
Handler
Middleware registration and execution handler.
class HandlerThis class wraps middleware metadata, including constructor arguments and route/method matching constraints, and provides execution orchestration for middleware instances.
It caches middleware objects per class name to avoid repeated construction and dispatches each middleware through its handle() method with the active request and downstream callable.
Properties
name
Name of the middleware.
public string $nameType: string
args
Arguments passed to the middleware.
public array $argsType: array
methods
Array of methods this middleware applies to.
private array $methodsType: array
exceptMethods
Array of methods this middleware does not apply to.
private array $exceptMethodsType: array
middlewareInstances
Static array to hold middleware instances.
private array $middlewareInstancesType: array
This is used to store instances of middleware classes to avoid creating multiple instances of the same middleware class.
Methods
__construct
Constructor to initialize the middleware handler.
public __construct(Middleware|string $middleware, array $args): voidParameters
| Parameter | Type | Description |
|---|---|---|
$middleware | Hazaar\Middleware\Interface\Middleware | string |
$args | array | Arguments for the middleware |
run
Executes the middleware by either reusing an existing instance or creating a new one,
public run(Request $request, callable $next): ResponseReturns: Hazaar\Controller\Response
then calls its 'handle' method with the provided request, next callable, and any additional arguments.
Parameters
| Parameter | Type | Description |
|---|---|---|
$request | Hazaar\Application\Request | the current HTTP request object |
$next | callable | the next middleware or handler to be executed |
only
Specifies that this middleware should only apply to the given HTTP method.
public only(string $method): selfReturns: self
Parameters
| Parameter | Type | Description |
|---|---|---|
$method | string | The HTTP method (e.g., 'GET', 'POST') to which the middleware should be restricted. |
except
Excludes the specified HTTP method from being processed by this middleware.
public except(string $method): selfReturns: self
Parameters
| Parameter | Type | Description |
|---|---|---|
$method | string | The HTTP method to exclude (e.g., 'GET', 'POST'). |
Determines if the given action name matches the middleware's method rules.
public (string $actionName): boolReturns: bool
Checks if the provided action name is included in the allowed methods and not in the excepted methods. If no specific methods are set, only checks against the excepted methods.
Parameters
| Parameter | Type | Description |
|---|---|---|
$actionName | string | the name of the action to check |
getName
public getName(): stringReturns: string
Generated by Hazaar API Doc Generator on Fri, 04 Sep 2026 23:32:17 +0000