Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. TwigExtension.php

function TwigExtension::getUrl

Generates an absolute URL given a route name and parameters.

@todo Add an option for scheme-relative URLs.

Parameters

$name: The name of the route.

array $parameters: An associative array of route parameter names and values.

array $options: (optional) An associative array of additional options. The 'absolute' option is forced to be TRUE.

Return value

array A render array with generated absolute URL for the given route.

File

core/lib/Drupal/Core/Template/TwigExtension.php, line 230

Class

TwigExtension
A class providing Drupal Twig extensions.

Namespace

Drupal\Core\Template

Code

public function getUrl($name, $parameters = [], $options = []) {
    assert($this->urlGenerator instanceof UrlGeneratorInterface, "The URL generator hasn't been set up. Any configuration YAML file with a service directive dealing with the Twig configuration can cause this, most likely found in a recently installed or changed module.");
    // Generate URL.
    $options['absolute'] = TRUE;
    $generated_url = $this->urlGenerator
        ->generateFromRoute($name, $parameters, $options, TRUE);
    // Return as render array, so we can bubble the bubbleable metadata.
    $build = [
        '#markup' => $generated_url->getGeneratedUrl(),
    ];
    $generated_url->applyTo($build);
    return $build;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal