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

Breadcrumb

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

function Request::create

Creates a Request based on a given URI and configuration.

The information contained in the URI always take precedence over the other information (server and parameters).

Parameters

string $uri The URI:

string $method The HTTP method:

array $parameters The query (GET) or request (POST) parameters:

array $cookies The request cookies ($_COOKIE):

array $files The request files ($_FILES):

array $server The server parameters ($_SERVER):

string|resource|null $content The raw body data:

Throws

BadRequestException When the URI is invalid

13 calls to Request::create()
AbstractSurrogate::handle in vendor/symfony/http-kernel/HttpCache/AbstractSurrogate.php
Handles a Surrogate from the cache.
AccessAwareRouter::match in core/lib/Drupal/Core/Routing/AccessAwareRouter.php
CommentController::commentPermalink in core/modules/comment/src/Controller/CommentController.php
Redirects comment links to the correct page depending on comment settings.
FileUpload::handleFileUploadForExistingResource in core/modules/jsonapi/src/Controller/FileUpload.php
Handles JSON:API file upload requests.
FunctionalTestSetupTrait::prepareRequestForGenerator in core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php
Creates a mock request and sets it on the generator.

... See full list

File

vendor/symfony/http-foundation/Request.php, line 282

Class

Request
Request represents an HTTP request.

Namespace

Symfony\Component\HttpFoundation

Code

public static function create(string $uri, string $method = 'GET', array $parameters = [], array $cookies = [], array $files = [], array $server = [], $content = null) : static {
    $server = array_replace([
        'SERVER_NAME' => 'localhost',
        'SERVER_PORT' => 80,
        'HTTP_HOST' => 'localhost',
        'HTTP_USER_AGENT' => 'Symfony',
        'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5',
        'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
        'REMOTE_ADDR' => '127.0.0.1',
        'SCRIPT_NAME' => '',
        'SCRIPT_FILENAME' => '',
        'SERVER_PROTOCOL' => 'HTTP/1.1',
        'REQUEST_TIME' => time(),
        'REQUEST_TIME_FLOAT' => microtime(true),
    ], $server);
    $server['PATH_INFO'] = '';
    $server['REQUEST_METHOD'] = strtoupper($method);
    if (false === ($components = parse_url(\strlen($uri) !== strcspn($uri, '?#') ? $uri : $uri . '#'))) {
        throw new BadRequestException('Invalid URI.');
    }
    if (false !== ($i = strpos($uri, '\\')) && $i < strcspn($uri, '?#')) {
        throw new BadRequestException('Invalid URI: A URI cannot contain a backslash.');
    }
    if (\strlen($uri) !== strcspn($uri, "\r\n\t")) {
        throw new BadRequestException('Invalid URI: A URI cannot contain CR/LF/TAB characters.');
    }
    if ('' !== $uri && (\ord($uri[0]) <= 32 || \ord($uri[-1]) <= 32)) {
        throw new BadRequestException('Invalid URI: A URI must not start nor end with ASCII control characters or spaces.');
    }
    if (isset($components['host'])) {
        $server['SERVER_NAME'] = $components['host'];
        $server['HTTP_HOST'] = $components['host'];
    }
    if (isset($components['scheme'])) {
        if ('https' === $components['scheme']) {
            $server['HTTPS'] = 'on';
            $server['SERVER_PORT'] = 443;
        }
        else {
            unset($server['HTTPS']);
            $server['SERVER_PORT'] = 80;
        }
    }
    if (isset($components['port'])) {
        $server['SERVER_PORT'] = $components['port'];
        $server['HTTP_HOST'] .= ':' . $components['port'];
    }
    if (isset($components['user'])) {
        $server['PHP_AUTH_USER'] = $components['user'];
    }
    if (isset($components['pass'])) {
        $server['PHP_AUTH_PW'] = $components['pass'];
    }
    if (!isset($components['path'])) {
        $components['path'] = '/';
    }
    switch (strtoupper($method)) {
        case 'POST':
        case 'PUT':
        case 'DELETE':
            if (!isset($server['CONTENT_TYPE'])) {
                $server['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
            }
        // no break
        case 'PATCH':
            $request = $parameters;
            $query = [];
            break;
        default:
            $request = [];
            $query = $parameters;
            break;
    }
    $queryString = '';
    if (isset($components['query'])) {
        parse_str(html_entity_decode($components['query']), $qs);
        if ($query) {
            $query = array_replace($qs, $query);
            $queryString = http_build_query($query, '', '&');
        }
        else {
            $query = $qs;
            $queryString = $components['query'];
        }
    }
    elseif ($query) {
        $queryString = http_build_query($query, '', '&');
    }
    $server['REQUEST_URI'] = $components['path'] . ('' !== $queryString ? '?' . $queryString : '');
    $server['QUERY_STRING'] = $queryString;
    return self::createRequestFromFactory($query, $request, [], $cookies, $files, $server, $content);
}

API Navigation

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