function UrlGenerator::generate
Same name in this branch
- 11.1.x core/lib/Drupal/Core/Routing/UrlGenerator.php \Drupal\Core\Routing\UrlGenerator::generate()
Overrides UrlGeneratorInterface::generate
1 method overrides UrlGenerator::generate()
- CompiledUrlGenerator::generate in vendor/
symfony/ routing/ Generator/ CompiledUrlGenerator.php - Generates a URL or path for a specific route based on the given parameters.
File
-
vendor/
symfony/ routing/ Generator/ UrlGenerator.php, line 104
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
public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH) : string {
$route = null;
$locale = $parameters['_locale'] ?? $this->context
->getParameter('_locale') ?: $this->defaultLocale;
if (null !== $locale) {
do {
if (null !== ($route = $this->routes
->get($name . '.' . $locale)) && $route->getDefault('_canonical_route') === $name) {
break;
}
} while (false !== ($locale = strstr($locale, '_', true)));
}
if (null === ($route ??= $this->routes
->get($name))) {
throw new RouteNotFoundException(\sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
}
// the Route has a cache of its own and is not recompiled as long as it does not get modified
$compiledRoute = $route->compile();
$defaults = $route->getDefaults();
$variables = $compiledRoute->getVariables();
if (isset($defaults['_canonical_route']) && isset($defaults['_locale'])) {
if (!\in_array('_locale', $variables, true)) {
unset($parameters['_locale']);
}
elseif (!isset($parameters['_locale'])) {
$parameters['_locale'] = $defaults['_locale'];
}
}
return $this->doGenerate($variables, $defaults, $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $name, $referenceType, $compiledRoute->getHostTokens(), $route->getSchemes());
}