Final Class Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher
| Inheritance | Yiisoft\ |
|---|
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| dispatch() | Dispatch request through middleware to get response. | Yiisoft\ |
| hasMiddlewares() | Yiisoft\ |
|
| withMiddlewares() | Returns new instance with middleware handlers replaced with the ones provided. | Yiisoft\ |
Method Details
| public mixed __construct ( Yiisoft\ | ||
| $middlewareFactory | Yiisoft\ |
|
| $eventDispatcher | ?\ |
|
public function __construct(
private MiddlewareFactory $middlewareFactory,
private ?EventDispatcherInterface $eventDispatcher = null,
) {}
Dispatch request through middleware to get response.
| public \ | ||
| $request | \ |
Request to pass to middleware. |
| $fallbackHandler | \ |
Handler to use in case no middleware produced response. |
public function dispatch(
ServerRequestInterface $request,
RequestHandlerInterface $fallbackHandler,
): ResponseInterface {
if ($this->stack === null) {
$this->stack = new MiddlewareStack($this->buildMiddlewares(), $fallbackHandler, $this->eventDispatcher);
}
return $this->stack->handle($request);
}
| public boolean hasMiddlewares ( ) | ||
| return | boolean |
Whether there are middleware defined in the dispatcher. |
|---|---|---|
public function hasMiddlewares(): bool
{
return $this->middlewareDefinitions !== [];
}
Returns new instance with middleware handlers replaced with the ones provided.
Last specified handler will be executed first.
| public self withMiddlewares ( array[]|callable[]|string[] $middlewareDefinitions ) | ||
| $middlewareDefinitions | array[]|callable[]|string[] |
Each array element is:
For handler action and callable
typed parameters are automatically injected using dependency injection container.
Current request and handler could be obtained by type-hinting for {@see \ |
public function withMiddlewares(array $middlewareDefinitions): self
{
$new = clone $this;
$new->middlewareDefinitions = array_reverse($middlewareDefinitions);
// Fixes a memory leak.
unset($new->stack);
$new->stack = null;
return $new;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.