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

Breadcrumb

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

function ServerRequest::getUriFromGlobals

Get a Uri populated with values from $_SERVER.

1 call to ServerRequest::getUriFromGlobals()
ServerRequest::fromGlobals in vendor/guzzlehttp/psr7/src/ServerRequest.php
Return a ServerRequest populated with superglobals: $_GET $_POST $_COOKIE $_FILES $_SERVER

File

vendor/guzzlehttp/psr7/src/ServerRequest.php, line 200

Class

ServerRequest
Server-side HTTP request

Namespace

GuzzleHttp\Psr7

Code

public static function getUriFromGlobals() : UriInterface {
    $uri = new Uri('');
    $uri = $uri->withScheme(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http');
    $hasPort = false;
    if (isset($_SERVER['HTTP_HOST'])) {
        [
            $host,
            $port,
        ] = self::extractHostAndPortFromAuthority($_SERVER['HTTP_HOST']);
        if ($host !== null) {
            $uri = $uri->withHost($host);
        }
        if ($port !== null) {
            $hasPort = true;
            $uri = $uri->withPort($port);
        }
    }
    elseif (isset($_SERVER['SERVER_NAME'])) {
        $uri = $uri->withHost($_SERVER['SERVER_NAME']);
    }
    elseif (isset($_SERVER['SERVER_ADDR'])) {
        $uri = $uri->withHost($_SERVER['SERVER_ADDR']);
    }
    if (!$hasPort && isset($_SERVER['SERVER_PORT'])) {
        $uri = $uri->withPort($_SERVER['SERVER_PORT']);
    }
    $hasQuery = false;
    if (isset($_SERVER['REQUEST_URI'])) {
        $requestUriParts = explode('?', $_SERVER['REQUEST_URI'], 2);
        $uri = $uri->withPath($requestUriParts[0]);
        if (isset($requestUriParts[1])) {
            $hasQuery = true;
            $uri = $uri->withQuery($requestUriParts[1]);
        }
    }
    if (!$hasQuery && isset($_SERVER['QUERY_STRING'])) {
        $uri = $uri->withQuery($_SERVER['QUERY_STRING']);
    }
    return $uri;
}

API Navigation

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