public UrlGenerator::generateFromRoute($name, $parameters = array(), $options = array(), $collect_bubbleable_metadata = FALSE)
Generates a URL or path for a specific route based on the given parameters.
Parameters that reference placeholders in the route pattern will be substituted for them in the pattern. Extra params are added as query strings to the URL.
@internal Should not be used in user code. Use \Drupal\Core\Url instead.
string|\Symfony\Component\Routing\Route $name: The route name or a route object.
array $parameters: An associative array of parameter names and values.
array $options: (optional) An associative array of additional options, with the following elements:
bool $collect_bubbleable_metadata: (optional) Defaults to FALSE. When TRUE, both the generated URL and its associated bubbleable metadata are returned.
string|\Drupal\Core\GeneratedUrl The generated URL for the given route. When $collect_bubbleable_metadata is TRUE, a GeneratedUrl object is returned, containing the generated URL plus bubbleable metadata.
\Symfony\Component\Routing\Exception\RouteNotFoundException Thrown when the named route does not exist.
\Symfony\Component\Routing\Exception\MissingMandatoryParametersException Thrown when some parameters are missing that are mandatory for the route.
\Symfony\Component\Routing\Exception\InvalidParameterException Thrown when a parameter value for a placeholder is not correct because it does not match the requirement.
Overrides UrlGeneratorInterface::generateFromRoute
public function generateFromRoute($name, $parameters = array(), $options = array(), $collect_bubbleable_metadata = FALSE) { $options += array('prefix' => ''); $route = $this->getRoute($name); $generated_url = $collect_bubbleable_metadata ? new GeneratedUrl() : NULL; $query_params = []; // Symfony adds any parameters that are not path slugs as query strings. if (isset($options['query']) && is_array($options['query'])) { $query_params = $options['query']; } $fragment = ''; if (isset($options['fragment'])) { if (($fragment = trim($options['fragment'])) != '') { $fragment = '#' . $fragment; } } // Generate a relative URL having no path, just query string and fragment. if ($route->getOption('_no_path')) { $query = $query_params ? '?' . http_build_query($query_params, '', '&') : ''; $url = $query . $fragment; return $collect_bubbleable_metadata ? $generated_url->setGeneratedUrl($url) : $url; } $options += $route->getOption('default_url_options') ? : []; $options += array('prefix' => '', 'path_processing' => TRUE); $name = $this->getRouteDebugMessage($name); $this->processRoute($name, $route, $parameters, $generated_url); $path = $this->getInternalPathFromRoute($name, $route, $parameters, $query_params); // Outbound path processors might need the route object for the path, e.g. // to get the path pattern. $options['route'] = $route; if ($options['path_processing']) { $path = $this->processPath($path, $options, $generated_url); } if (!empty($options['prefix'])) { $path = ltrim($path, '/'); $prefix = empty($path) ? rtrim($options['prefix'], '/') : $options['prefix']; $path = '/' . str_replace('%2F', '/', rawurlencode($prefix)) . $path; } // The base_url might be rewritten from the language rewrite in domain mode. if (isset($options['base_url'])) { $base_url = $options['base_url']; if (isset($options['https'])) { if ($options['https'] === TRUE) { $base_url = str_replace('http://', 'https://', $base_url); } elseif ($options['https'] === FALSE) { $base_url = str_replace('https://', 'http://', $base_url); } } $url = $base_url . $path . $fragment; return $collect_bubbleable_metadata ? $generated_url->setGeneratedUrl($url) : $url; } $base_url = $this->context->getBaseUrl(); $absolute = !empty($options['absolute']); if (!$absolute || !$host = $this->context->getHost()) { $url = $base_url . $path . $fragment; return $collect_bubbleable_metadata ? $generated_url->setGeneratedUrl($url) : $url; } // Prepare an absolute URL by getting the correct scheme, host and port from // the request context. if (isset($options['https'])) { $scheme = $options['https'] ? 'https' : 'http'; } else { $scheme = $this->context->getScheme(); } $scheme_req = $route->getSchemes(); if ($scheme_req && ($req = $scheme_req[0]) && $scheme !== $req) { $scheme = $req; } $port = ''; if ('http' === $scheme && 80 != $this->context->getHttpPort()) { $port = ':' . $this->context->getHttpPort(); } elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) { $port = ':' . $this->context->getHttpsPort(); } if ($collect_bubbleable_metadata) { $generated_url->addCacheContexts(['url.site']); } $url = $scheme . '://' . $host . $port . $base_url . $path . $fragment; return $collect_bubbleable_metadata ? $generated_url->setGeneratedUrl($url) : $url; }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Routing!UrlGenerator.php/function/UrlGenerator::generateFromRoute/8.1.x