0 follower

Final Class Yiisoft\Router\Middleware\Router

InheritanceYiisoft\Router\Middleware\Router
ImplementsPsr\Http\Server\MiddlewareInterface

Method Details

Hide inherited methods

__construct() public method

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);
}

            
process() public method

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);
}