Final Class Yiisoft\Router\Middleware\Router
| Inheritance | Yiisoft\Router\Middleware\Router |
|---|---|
| Implements | Psr\Http\Server\MiddlewareInterface |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Router\Middleware\Router | |
| process() | Yiisoft\Router\Middleware\Router |
Method Details
| public mixed __construct ( Yiisoft\Router\UrlMatcherInterface $matcher, \Psr\Http\Message\ResponseFactoryInterface $responseFactory, \Yiisoft\Middleware\Dispatcher\MiddlewareFactory $middlewareFactory, Yiisoft\Router\CurrentRoute $currentRoute, \Psr\EventDispatcher\EventDispatcherInterface|null $eventDispatcher = null ) | ||
| $matcher | Yiisoft\Router\UrlMatcherInterface | |
| $responseFactory | \Psr\Http\Message\ResponseFactoryInterface | |
| $middlewareFactory | \Yiisoft\Middleware\Dispatcher\MiddlewareFactory | |
| $currentRoute | Yiisoft\Router\CurrentRoute | |
| $eventDispatcher | \Psr\EventDispatcher\EventDispatcherInterface|null | |
public function __construct(
private readonly UrlMatcherInterface $matcher,
private readonly ResponseFactoryInterface $responseFactory,
MiddlewareFactory $middlewareFactory,
private readonly CurrentRoute $currentRoute,
?EventDispatcherInterface $eventDispatcher = null
) {
$this->dispatcher = new MiddlewareDispatcher($middlewareFactory, $eventDispatcher);
}
| public \Psr\Http\Message\ResponseInterface process ( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $handler ) | ||
| $request | \Psr\Http\Message\ServerRequestInterface | |
| $handler | \Psr\Http\Server\RequestHandlerInterface | |
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$result = $this->matcher->match($request);
$this->currentRoute->setUri($request->getUri());
if ($result->isMethodFailure()) {
if ($request->getMethod() === Method::OPTIONS) {
return $this->responseFactory
->createResponse(Status::NO_CONTENT)
->withHeader('Allow', implode(', ', $result->methods()));
}
return $this->responseFactory
->createResponse(Status::METHOD_NOT_ALLOWED)
->withHeader('Allow', implode(', ', $result->methods()));
}
if (!$result->isSuccess()) {
return $handler->handle($request);
}
$this->currentRoute->setRouteWithArguments($result->route(), $result->arguments());
return $this->dispatcher
->withMiddlewares($result->route()->getData('enabledMiddlewares'))
->dispatch($request, $handler);
}
Signup or Login in order to comment.