You are here

protected function RedirectResponseSubscriber::getDestinationAsAbsoluteUrl in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/EventSubscriber/RedirectResponseSubscriber.php \Drupal\Core\EventSubscriber\RedirectResponseSubscriber::getDestinationAsAbsoluteUrl()
  2. 9 core/lib/Drupal/Core/EventSubscriber/RedirectResponseSubscriber.php \Drupal\Core\EventSubscriber\RedirectResponseSubscriber::getDestinationAsAbsoluteUrl()

Converts the passed in destination into an absolute URL.

Parameters

string $destination: The path for the destination. In case it starts with a slash it should have the base path included already.

string $scheme_and_host: The scheme and host string of the current request.

Return value

string The destination as absolute URL.

File

core/lib/Drupal/Core/EventSubscriber/RedirectResponseSubscriber.php, line 104

Class

RedirectResponseSubscriber
Allows manipulation of the response object when performing a redirect.

Namespace

Drupal\Core\EventSubscriber

Code

protected function getDestinationAsAbsoluteUrl($destination, $scheme_and_host) {
  if (!UrlHelper::isExternal($destination)) {

    // The destination query parameter can be a relative URL in the sense of
    // not including the scheme and host, but its path is expected to be
    // absolute (start with a '/'). For such a case, prepend the scheme and
    // host, because the 'Location' header must be absolute.
    if (strpos($destination, '/') === 0) {
      $destination = $scheme_and_host . $destination;
    }
    else {

      // Legacy destination query parameters can be internal paths that have
      // not yet been converted to URLs.
      $destination = UrlHelper::parse($destination);
      $uri = 'base:' . $destination['path'];
      $options = [
        'query' => $destination['query'],
        'fragment' => $destination['fragment'],
        'absolute' => TRUE,
      ];

      // Treat this as if it's user input of a path relative to the site's
      // base URL.
      $destination = $this->unroutedUrlAssembler
        ->assemble($uri, $options);
    }
  }
  return $destination;
}