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

Breadcrumb

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

function Uri::composeComponents

Composes a URI reference string from its various components.

Usually this method does not need to be called manually but instead is used indirectly via `Psr\Http\Message\UriInterface::__toString`.

PSR-7 UriInterface treats an empty component the same as a missing component as getQuery(), getFragment() etc. always return a string. This explains the slight difference to RFC 3986 Section 5.3.

Another adjustment is that the authority separator is added even when the authority is missing/empty for the "file" scheme. This is because PHP stream functions like `file_get_contents` only work with `file:///myfile` but not with `file:/myfile` although they are equivalent according to RFC 3986. But `file:///` is the more common syntax for the file scheme anyway (Chrome for example redirects to that format).

See also

https://datatracker.ietf.org/doc/html/rfc3986#section-5.3

2 calls to Uri::composeComponents()
Uri::__toString in vendor/guzzlehttp/psr7/src/Uri.php
Return the string representation as a URI reference.
UriResolver::resolve in vendor/guzzlehttp/psr7/src/UriResolver.php
Converts the relative URI into a new URI that is resolved against the base URI.

File

vendor/guzzlehttp/psr7/src/Uri.php, line 167

Class

Uri
PSR-7 URI implementation.

Namespace

GuzzleHttp\Psr7

Code

public static function composeComponents(?string $scheme, ?string $authority, string $path, ?string $query, ?string $fragment) : string {
    $uri = '';
    // weak type checks to also accept null until we can add scalar type hints
    if ($scheme != '') {
        $uri .= $scheme . ':';
    }
    if ($authority != '' || $scheme === 'file') {
        $uri .= '//' . $authority;
    }
    if ($authority != '' && $path != '' && $path[0] != '/') {
        $path = '/' . $path;
    }
    $uri .= $path;
    if ($query != '') {
        $uri .= '?' . $query;
    }
    if ($fragment != '') {
        $uri .= '#' . $fragment;
    }
    return $uri;
}

API Navigation

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