function UrlGenerator::doGenerate
Same name in this branch
- 11.1.x core/lib/Drupal/Core/Routing/UrlGenerator.php \Drupal\Core\Routing\UrlGenerator::doGenerate()
Throws
MissingMandatoryParametersException When some parameters are missing that are mandatory for the route
InvalidParameterException When a parameter value for a placeholder is not correct because it does not match the requirement
2 calls to UrlGenerator::doGenerate()
- CompiledUrlGenerator::generate in vendor/
symfony/ routing/ Generator/ CompiledUrlGenerator.php - Generates a URL or path for a specific route based on the given parameters.
- UrlGenerator::generate in vendor/
symfony/ routing/ Generator/ UrlGenerator.php - Generates a URL or path for a specific route based on the given parameters.
File
-
vendor/
symfony/ routing/ Generator/ UrlGenerator.php, line 143
Class
- UrlGenerator
- UrlGenerator can generate a URL or a path for any route in the RouteCollection based on the passed parameters.
Namespace
Symfony\Component\Routing\GeneratorCode
protected function doGenerate(array $variables, array $defaults, array $requirements, array $tokens, array $parameters, string $name, int $referenceType, array $hostTokens, array $requiredSchemes = []) : string {
$variables = array_flip($variables);
$mergedParams = array_replace($defaults, $this->context
->getParameters(), $parameters);
// all params must be given
if ($diff = array_diff_key($variables, $mergedParams)) {
throw new MissingMandatoryParametersException($name, array_keys($diff));
}
$url = '';
$optional = true;
$message = 'Parameter "{parameter}" for route "{route}" must match "{expected}" ("{given}" given) to generate a corresponding URL.';
foreach ($tokens as $token) {
if ('variable' === $token[0]) {
$varName = $token[3];
// variable is not important by default
$important = $token[5] ?? false;
if (!$optional || $important || !\array_key_exists($varName, $defaults) || null !== $mergedParams[$varName] && (string) $mergedParams[$varName] !== (string) $defaults[$varName]) {
// check requirement (while ignoring look-around patterns)
if (null !== $this->strictRequirements && !preg_match('#^' . preg_replace('/\\(\\?(?:=|<=|!|<!)((?:[^()\\\\]+|\\\\.|\\((?1)\\))*)\\)/', '', $token[2]) . '$#i' . (empty($token[4]) ? '' : 'u'), $mergedParams[$token[3]] ?? '')) {
if ($this->strictRequirements) {
throw new InvalidParameterException(strtr($message, [
'{parameter}' => $varName,
'{route}' => $name,
'{expected}' => $token[2],
'{given}' => $mergedParams[$varName],
]));
}
$this->logger?->error($message, [
'parameter' => $varName,
'route' => $name,
'expected' => $token[2],
'given' => $mergedParams[$varName],
]);
return '';
}
$url = $token[1] . $mergedParams[$varName] . $url;
$optional = false;
}
}
else {
// static text
$url = $token[1] . $url;
$optional = false;
}
}
if ('' === $url) {
$url = '/';
}
// the contexts base URL is already encoded (see Symfony\Component\HttpFoundation\Request)
$url = strtr(rawurlencode($url), $this->decodedChars);
// the path segments "." and ".." are interpreted as relative reference when resolving a URI; see http://tools.ietf.org/html/rfc3986#section-3.3
// so we need to encode them as they are not used for this purpose here
// otherwise we would generate a URI that, when followed by a user agent (e.g. browser), does not match this route
$url = strtr($url, [
'/../' => '/%2E%2E/',
'/./' => '/%2E/',
]);
if (str_ends_with($url, '/..')) {
$url = substr($url, 0, -2) . '%2E%2E';
}
elseif (str_ends_with($url, '/.')) {
$url = substr($url, 0, -1) . '%2E';
}
$schemeAuthority = '';
$host = $this->context
->getHost();
$scheme = $this->context
->getScheme();
if ($requiredSchemes) {
if (!\in_array($scheme, $requiredSchemes, true)) {
$referenceType = self::ABSOLUTE_URL;
$scheme = current($requiredSchemes);
}
}
if ($hostTokens) {
$routeHost = '';
foreach ($hostTokens as $token) {
if ('variable' === $token[0]) {
// check requirement (while ignoring look-around patterns)
if (null !== $this->strictRequirements && !preg_match('#^' . preg_replace('/\\(\\?(?:=|<=|!|<!)((?:[^()\\\\]+|\\\\.|\\((?1)\\))*)\\)/', '', $token[2]) . '$#i' . (empty($token[4]) ? '' : 'u'), $mergedParams[$token[3]])) {
if ($this->strictRequirements) {
throw new InvalidParameterException(strtr($message, [
'{parameter}' => $token[3],
'{route}' => $name,
'{expected}' => $token[2],
'{given}' => $mergedParams[$token[3]],
]));
}
$this->logger?->error($message, [
'parameter' => $token[3],
'route' => $name,
'expected' => $token[2],
'given' => $mergedParams[$token[3]],
]);
return '';
}
$routeHost = $token[1] . $mergedParams[$token[3]] . $routeHost;
}
else {
$routeHost = $token[1] . $routeHost;
}
}
if ($routeHost !== $host) {
$host = $routeHost;
if (self::ABSOLUTE_URL !== $referenceType) {
$referenceType = self::NETWORK_PATH;
}
}
}
if (self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) {
if ('' !== $host || '' !== $scheme && 'http' !== $scheme && 'https' !== $scheme) {
$port = '';
if ('http' === $scheme && 80 !== $this->context
->getHttpPort()) {
$port = ':' . $this->context
->getHttpPort();
}
elseif ('https' === $scheme && 443 !== $this->context
->getHttpsPort()) {
$port = ':' . $this->context
->getHttpsPort();
}
$schemeAuthority = self::NETWORK_PATH === $referenceType || '' === $scheme ? '//' : "{$scheme}://";
$schemeAuthority .= $host . $port;
}
}
if (self::RELATIVE_PATH === $referenceType) {
$url = self::getRelativePath($this->context
->getPathInfo(), $url);
}
else {
$url = $schemeAuthority . $this->context
->getBaseUrl() . $url;
}
// add a query string if needed
$extra = array_udiff_assoc(array_diff_key($parameters, $variables), $defaults, fn($a, $b) => $a == $b ? 0 : 1);
array_walk_recursive($extra, $caster = static function (&$v) use (&$caster) {
if (\is_object($v)) {
if ($vars = get_object_vars($v)) {
array_walk_recursive($vars, $caster);
$v = $vars;
}
elseif ($v instanceof \Stringable) {
$v = (string) $v;
}
}
});
// extract fragment
$fragment = $defaults['_fragment'] ?? '';
if (isset($extra['_fragment'])) {
$fragment = $extra['_fragment'];
unset($extra['_fragment']);
}
if ($extra && ($query = http_build_query($extra, '', '&', \PHP_QUERY_RFC3986))) {
$url .= '?' . strtr($query, self::QUERY_FRAGMENT_DECODED);
}
if ('' !== $fragment) {
$url .= '#' . strtr(rawurlencode($fragment), self::QUERY_FRAGMENT_DECODED);
}
return $url;
}