0 follower

Final Class Yiisoft\Yii\Middleware\Subfolder

InheritanceYiisoft\Yii\Middleware\Subfolder
ImplementsPsr\Http\Server\MiddlewareInterface

This middleware supports routing when the entry point of the application isn't directly at the webroot.

By default, it determines webroot based on server parameters.

You should place this middleware before Route middleware in the middleware list.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\Router\UrlGeneratorInterface $uriGenerator, \Yiisoft\Aliases\Aliases $aliases, string|null $prefix null, string|null $baseUrlAlias '@baseUrl' )
$uriGenerator \Yiisoft\Router\UrlGeneratorInterface

The URI generator instance.

$aliases \Yiisoft\Aliases\Aliases

The aliases instance.

$prefix string|null

URI prefix that goes immediately after the domain part. The prefix value usually begins with a slash and mustn't end with a slash.

$baseUrlAlias string|null

The base URL alias {@see \Yiisoft\Aliases\Aliases::get()}. Defaults to @baseUrl.

                public function __construct(
    private readonly UrlGeneratorInterface $uriGenerator,
    private readonly Aliases $aliases,
    private readonly ?string $prefix = null,
    private readonly ?string $baseUrlAlias = '@baseUrl',
) {
}

            
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
{
    $uri = $request->getUri();
    $path = $uri->getPath();
    $baseUrl = $this->prefix ?? $this->getBaseUrl($request);
    $length = strlen($baseUrl);
    if ($this->prefix !== null) {
        if (empty($this->prefix)) {
            throw new BadUriPrefixException('URI prefix can\'t be empty.');
        }
        if ($baseUrl[-1] === '/') {
            throw new BadUriPrefixException('Wrong URI prefix value.');
        }
        if (!str_starts_with($path, $baseUrl)) {
            throw new BadUriPrefixException('URI prefix doesn\'t match.');
        }
    }
    if ($length > 0) {
        $newPath = substr($path, $length);
        if ($newPath === '') {
            $newPath = '/';
        }
        if ($newPath[0] === '/') {
            $request = $request->withUri($uri->withPath($newPath));
            $this->uriGenerator->setUriPrefix($baseUrl);
            if ($this->baseUrlAlias !== null && $this->prefix === null) {
                $this->aliases->set($this->baseUrlAlias, $baseUrl);
            }
        } elseif ($this->prefix !== null) {
            throw new BadUriPrefixException('URI prefix doesn\'t match completely.');
        }
    }
    return $handler->handle($request);
}