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

Breadcrumb

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

function UriResolver::resolve

Same name in this branch
  1. 11.1.x vendor/guzzlehttp/psr7/src/UriResolver.php \GuzzleHttp\Psr7\UriResolver::resolve()
  2. 11.1.x vendor/justinrainbow/json-schema/src/JsonSchema/Uri/UriResolver.php \JsonSchema\Uri\UriResolver::resolve()

Resolves a URI according to a base URI.

For example if $uri=/foo/bar and $baseUri=https://symfony.com it will return https://symfony.com/foo/bar

If the $uri is not absolute you must pass an absolute $baseUri

1 call to UriResolver::resolve()
AbstractUriElement::getUri in vendor/symfony/dom-crawler/AbstractUriElement.php
Gets the URI associated with this link.

File

vendor/symfony/dom-crawler/UriResolver.php, line 31

Class

UriResolver
The UriResolver class takes an URI (relative, absolute, fragment, etc.) and turns it into an absolute URI against another given base URI.

Namespace

Symfony\Component\DomCrawler

Code

public static function resolve(string $uri, ?string $baseUri) : string {
    $uri = trim($uri);
    // absolute URL?
    if (null !== parse_url(\strlen($uri) !== strcspn($uri, '?#') ? $uri : $uri . '#', \PHP_URL_SCHEME)) {
        return $uri;
    }
    if (null === $baseUri) {
        throw new \InvalidArgumentException('The URI is relative, so you must define its base URI passing an absolute URL.');
    }
    // empty URI
    if (!$uri) {
        return $baseUri;
    }
    // an anchor
    if ('#' === $uri[0]) {
        return self::cleanupAnchor($baseUri) . $uri;
    }
    $baseUriCleaned = self::cleanupUri($baseUri);
    if ('?' === $uri[0]) {
        return $baseUriCleaned . $uri;
    }
    // absolute URL with relative schema
    if (str_starts_with($uri, '//')) {
        return preg_replace('#^([^/]*)//.*$#', '$1', $baseUriCleaned) . $uri;
    }
    $baseUriCleaned = preg_replace('#^(.*?//[^/]*)(?:\\/.*)?$#', '$1', $baseUriCleaned);
    // absolute path
    if ('/' === $uri[0]) {
        return $baseUriCleaned . $uri;
    }
    // relative path
    $path = parse_url(substr($baseUri, \strlen($baseUriCleaned)), \PHP_URL_PATH) ?? '';
    $path = self::canonicalizePath(substr($path, 0, strrpos($path, '/')) . '/' . $uri);
    return $baseUriCleaned . ('' === $path || '/' !== $path[0] ? '/' : '') . $path;
}

API Navigation

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