Dispatcher
Dispatcher
Class MiddlewareDispatcher.
Responsible for managing and dispatching middleware components within the application. Handles the execution flow of middleware stacks, allowing for request and response manipulation at various stages of the application's lifecycle.
Properties
aliases
Stores aliases for middleware classes.
public array $aliases
middlewareStack
Stack of middleware components to be executed in order.
private array $middlewareStack
Methods
__construct
MiddlewareDispatcher constructor.
public __construct(array $middlwareAliases): void
Initializes the middleware dispatcher with an empty stack.
Optionally accepts a configuration array to load global middleware and aliases. This allows for dynamic configuration of middleware components and their aliases, enhancing the flexibility of the middleware system.
Parameters
Parameter | Type | Description |
---|---|---|
$middlwareAliases | array |
add
Adds a middleware instance to the middleware stack.
public add(string $middleware, mixed $args): void
Parameters
Parameter | Type | Description |
---|---|---|
$middleware | string | the middleware to add to the stack |
$args | mixed |
addFromArray
Adds an array of global middleware classes to the dispatcher.
public addFromArray(array $middlewareClasses): void
Each class in the provided array is validated to ensure it exists and implements the Middleware interface. If a class does not exist or does not implement the required interface, an InvalidArgumentException is thrown. Valid middleware classes are instantiated and added to the dispatcher.
Parameters
Parameter | Type | Description |
---|---|---|
$middlewareClasses | array | array of middleware class names to add |
addHandler
Adds a middleware handler to the middleware stack.
public addHandler(Handler $handler): void
Parameters
Parameter | Type | Description |
---|---|---|
$handler | Handler | the middleware handler to add to the stack |
addHandlers
Add multiple middleware handlers to the dispatcher.
public addHandlers(array $handlers): void
This method allows for adding multiple middleware handlers at once.
Parameters
Parameter | Type | Description |
---|---|---|
$handlers | array | an array of middleware handlers to add |
handle
Handles the incoming HTTP request by passing it through the middleware stack.
public handle(Request $request, callable $finalHandler): Response
This method reverses the middleware stack and wraps each middleware around the next, creating a chain of responsibility. The final handler is called after all middleware have been processed.
Parameters
Parameter | Type | Description |
---|---|---|
$request | Request | the incoming HTTP request to handle |
$finalHandler | callable | the final handler to be called after all middleware |
Generated by Hazaar API Doc Generator on Tue, 29 Jul 2025 08:01:32 +0000