0 follower

Final Class Yiisoft\Router\FastRoute\UrlGenerator

InheritanceYiisoft\Router\FastRoute\UrlGenerator
ImplementsYiisoft\Router\UrlGeneratorInterface

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\Router\RouteCollectionInterface $routeCollection, \Yiisoft\Router\CurrentRoute|null $currentRoute null, \FastRoute\RouteParser|null $parser null, string|null $scheme null, string|null $host null )
$routeCollection \Yiisoft\Router\RouteCollectionInterface
$currentRoute \Yiisoft\Router\CurrentRoute|null
$parser \FastRoute\RouteParser|null
$scheme string|null
$host string|null

                public function __construct(
    private readonly RouteCollectionInterface $routeCollection,
    private readonly ?CurrentRoute $currentRoute = null,
    ?RouteParser $parser = null,
    private readonly ?string $scheme = null,
    private readonly ?string $host = null,
) {
    $this->routeParser = $parser ?? new RouteParser\Std();
}

            
generate() public method

Replacements in FastRoute are written as {name} or {name:<pattern>}; this method uses {@see \FastRoute\RouteParser\Std} to search for the best route match based on the available substitutions and generates a URI.

public generate ( string $name, array $arguments = [], array $queryParameters = [], string|null $hash null )
$name string
$arguments array
$queryParameters array
$hash string|null
throws RuntimeException

If parameter value does not match its regex.

                public function generate(
    string $name,
    array $arguments = [],
    array $queryParameters = [],
    ?string $hash = null,
): string {
    $arguments = array_merge(
        $this->defaultArguments,
        $this->prepareArguments($arguments),
    );
    $route = $this->routeCollection->getRoute($name);
    /** @var list<list<list<string>|string>> $parsedRoutes */
    $parsedRoutes = array_reverse($this->routeParser->parse($route->getData('pattern')));
    if ($parsedRoutes === []) {
        throw new RouteNotFoundException($name);
    }
    $missingArguments = [];
    // One route pattern can correspond to multiple routes if it has optional parts.
    foreach ($parsedRoutes as $parsedRouteParts) {
        // Check if all arguments can be substituted
        $missingArguments = $this->missingArguments($parsedRouteParts, $arguments);
        // If not all arguments can be substituted, try the next route.
        if (!empty($missingArguments)) {
            continue;
        }
        return $this->generatePath($arguments, $queryParameters, $parsedRouteParts, $hash);
    }
    // No valid route was found: list minimal required parameters.
    throw new RuntimeException(
        sprintf(
            'Route `%s` expects at least argument values for [%s], but received [%s]',
            $name,
            implode(',', $missingArguments),
            implode(',', array_keys($arguments))
        )
    );
}

            
generateAbsolute() public method

public string generateAbsolute ( string $name, array $arguments = [], array $queryParameters = [], string|null $hash null, string|null $scheme null, string|null $host null )
$name string
$arguments array
$queryParameters array
$hash string|null
$scheme string|null
$host string|null

                public function generateAbsolute(
    string $name,
    array $arguments = [],
    array $queryParameters = [],
    ?string $hash = null,
    ?string $scheme = null,
    ?string $host = null,
): string {
    $url = $this->generate($name, $arguments, $queryParameters, $hash);
    $route = $this->routeCollection->getRoute($name);
    $uri = $this->currentRoute && $this->currentRoute->getUri() !== null ? $this->currentRoute->getUri() : null;
    $lastRequestScheme = $uri?->getScheme();
    $host ??= $route->getData('host') ?? $this->host ?? null;
    if ($host !== null) {
        $isRelativeHost = $this->isRelative($host);
        $scheme ??= $isRelativeHost
            ? $this->scheme ?? $lastRequestScheme
            : null;
        if ($scheme === null && !$isRelativeHost) {
            return rtrim($host, '/') . $url;
        }
        if ($host !== '' && $isRelativeHost) {
            $host = '//' . $host;
        }
        return $this->ensureScheme(rtrim($host, '/') . $url, $scheme);
    }
    return $uri === null ? $url : $this->generateAbsoluteFromLastMatchedRequest($url, $uri, $scheme);
}

            
generateFromCurrent() public method

public generateFromCurrent ( array $replacedArguments, array $queryParameters = [], string|null $hash null, string|null $fallbackRouteName null )
$replacedArguments array
$queryParameters array
$hash string|null
$fallbackRouteName string|null

                public function generateFromCurrent(
    array $replacedArguments,
    array $queryParameters = [],
    ?string $hash = null,
    ?string $fallbackRouteName = null,
): string {
    if ($this->currentRoute === null || $this->currentRoute->getName() === null) {
        if ($fallbackRouteName !== null) {
            return $this->generate($fallbackRouteName, $replacedArguments, hash: $hash);
        }
        if ($this->currentRoute !== null && $this->currentRoute->getUri() !== null) {
            return $this->currentRoute->getUri()->getPath() . ($hash !== null ? '#' . $hash : '');
        }
        throw new RuntimeException('Current route is not detected.');
    }
    if ($this->currentRoute->getUri() !== null) {
        $currentQueryParameters = [];
        parse_str($this->currentRoute->getUri()->getQuery(), $currentQueryParameters);
        $queryParameters = array_merge($currentQueryParameters, $queryParameters);
    }
    /** @psalm-suppress PossiblyNullArgument Checked route name on null above. */
    return $this->generate(
        $this->currentRoute->getName(),
        array_merge($this->currentRoute->getArguments(), $replacedArguments),
        $queryParameters,
        $hash,
    );
}

            
getUriPrefix() public method

public string getUriPrefix ( )

                public function getUriPrefix(): string
{
    return $this->uriPrefix;
}

            
setDefaultArgument() public method

public void setDefaultArgument ( string $name, mixed $value )
$name string
$value mixed

                public function setDefaultArgument(string $name, $value): void
{
    if (!is_scalar($value) && !$value instanceof Stringable && $value !== null) {
        throw new InvalidArgumentException('Default should be either scalar value or an instance of \Stringable.');
    }
    $this->defaultArguments[$name] = (string) $value;
}

            
setEncodeRaw() public method

public void setEncodeRaw ( boolean $encodeRaw )
$encodeRaw boolean

                public function setEncodeRaw(bool $encodeRaw): void
{
    $this->encodeRaw = $encodeRaw;
}

            
setUriPrefix() public method

public void setUriPrefix ( string $name )
$name string

                public function setUriPrefix(string $name): void
{
    $this->uriPrefix = $name;
}