function Url::toUriString

Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Url.php \Drupal\Core\Url::toUriString()
  2. 10 core/lib/Drupal/Core/Url.php \Drupal\Core\Url::toUriString()
  3. 11.x core/lib/Drupal/Core/Url.php \Drupal\Core\Url::toUriString()

Generates a URI string that represents the data in the Url object.

The URI will typically have the scheme of route: even if the object was constructed using an entity: or internal: scheme. An internal: URI that does not match a Drupal route with be returned here with the base: scheme, and external URLs will be returned in their original form.

Return value

string A URI representation of the Url object data.

File

core/lib/Drupal/Core/Url.php, line 524

Class

Url
Defines an object that holds information about a URL.

Namespace

Drupal\Core

Code

public function toUriString() {
    if ($this->isRouted()) {
        $uri = 'route:' . $this->routeName;
        if ($this->routeParameters) {
            $uri .= ';' . UrlHelper::buildQuery($this->routeParameters);
        }
    }
    else {
        $uri = $this->uri;
    }
    $query = !empty($this->options['query']) ? '?' . UrlHelper::buildQuery($this->options['query']) : '';
    $fragment = isset($this->options['fragment']) && strlen($this->options['fragment']) ? '#' . $this->options['fragment'] : '';
    return $uri . $query . $fragment;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.