public function UrlGenerator::generate

Same name in this branch
  1. 8.x drupal/core/lib/Drupal/Core/Routing/UrlGenerator.php \Drupal\Core\Routing\UrlGenerator::generate()
  2. 8.x drupal/core/vendor/symfony/routing/Symfony/Component/Routing/Generator/UrlGenerator.php \Symfony\Component\Routing\Generator\UrlGenerator::generate()

Implements Symfony\Component\Routing\Generator\UrlGeneratorInterface::generate().

Overrides ProviderBasedGenerator::generate

1 method overrides UrlGenerator::generate()
NullGenerator::generate in drupal/core/lib/Drupal/Core/Routing/NullGenerator.php
Overrides Drupal\Core\Routing\UrlGenerator::generate();

File

drupal/core/lib/Drupal/Core/Routing/UrlGenerator.php, line 111
Contains Drupal\Core\Routing\UrlGenerator.

Class

UrlGenerator
A Generator creates URL strings based on a specified route.

Namespace

Drupal\Core\Routing

Code

public function generate($name, $parameters = array(), $absolute = FALSE) {
  if ($name instanceof SymfonyRoute) {
    $route = $name;
  }
  elseif (null === ($route = $this->provider
    ->getRouteByName($name, $parameters))) {
    throw new RouteNotFoundException(sprintf('Route "%s" 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();
  $hostTokens = $compiledRoute
    ->getHostTokens();
  $route_requirements = $route
    ->getRequirements();

  // We need to bypass the doGenerate() method's handling of absolute URLs as
  // we handle that ourselves after processing the path.
  if (isset($route_requirements['_scheme'])) {
    $scheme_req = $route_requirements['_scheme'];
    unset($route_requirements['_scheme']);
  }
  $path = $this
    ->doGenerate($compiledRoute
    ->getVariables(), $route
    ->getDefaults(), $route_requirements, $compiledRoute
    ->getTokens(), $parameters, $name, FALSE, $hostTokens);

  // The URL returned from doGenerate() will include the base path if there is
  // one (i.e., if running in a subdirectory) so we need to strip that off
  // before processing the path.
  $base_url = $this->context
    ->getBaseUrl();
  if (!empty($base_url) && strpos($path, $base_url) === 0) {
    $path = substr($path, strlen($base_url));
  }
  $path = $this
    ->processPath($path);
  if (!$absolute || !($host = $this->context
    ->getHost())) {
    return $base_url . $path;
  }

  // Prepare an absolute URL by getting the correct scheme, host and port from
  // the request context.
  $scheme = $this->context
    ->getScheme();
  if (isset($scheme_req) && ($req = strtolower($scheme_req)) && $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();
  }
  return $scheme . '://' . $host . $port . $base_url . $path;
}