Symfony\Component\HttpFoundation\Request::create PHP Method

create() public static method

The information contained in the URI always take precedence over the other information (server and parameters).
public static create ( string $uri, string $method = 'GET', array $parameters = [], array $cookies = [], array $files = [], array $server = [], string $content = null ) : Request
$uri string The URI
$method string The HTTP method
$parameters array The query (GET) or request (POST) parameters
$cookies array The request cookies ($_COOKIE)
$files array The request files ($_FILES)
$server array The server parameters ($_SERVER)
$content string The raw body data
return Request A Request instance
    public static function create($uri, $method = 'GET', $parameters = array(), $cookies = array(), $files = array(), $server = array(), $content = null)
    {
        $server = array_replace(array(
            'SERVER_NAME' => 'localhost',
            'SERVER_PORT' => 80,
            'HTTP_HOST' => 'localhost',
            'HTTP_USER_AGENT' => 'Symfony/3.X',
            '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(),
        ), $server);

        $server['PATH_INFO'] = '';
        $server['REQUEST_METHOD'] = strtoupper($method);

        $components = parse_url($uri);
        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'] = $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 = array();
                break;
            default:
                $request = array();
                $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, array(), $cookies, $files, $server, $content);
    }

Usage Example

Example #1
1
 /**
  * Perform an action on a Contenttype record.
  *
  * The action part of the POST request should take the form:
  * [
  *     contenttype => [
  *         id => [
  *             action => [field => value]
  *         ]
  *     ]
  * ]
  *
  * For example:
  * [
  *     'pages'   => [
  *         3 => ['modify' => ['status' => 'held']],
  *         5 => null,
  *         4 => ['modify' => ['status' => 'draft']],
  *         1 => ['delete' => null],
  *         2 => ['modify' => ['status' => 'published']],
  *     ],
  *     'entries' => [
  *         4 => ['modify' => ['status' => 'published']],
  *         1 => null,
  *         5 => ['delete' => null],
  *         2 => null,
  *         3 => ['modify' => ['title' => 'Drop Bear Attacks']],
  *     ]
  * ]
  *
  * @param Request $request Symfony Request
  *
  * @return Response
  */
 public function action(Request $request)
 {
     //         if (!$this->checkAntiCSRFToken($request->get('bolt_csrf_token'))) {
     //             $this->app->abort(Response::HTTP_BAD_REQUEST, Trans::__('Something went wrong'));
     //         }
     $contentType = $request->get('contenttype');
     $actionData = $request->get('actions');
     if ($actionData === null) {
         throw new \UnexpectedValueException('No content action data provided in the request.');
     }
     foreach ($actionData as $contentTypeSlug => $recordIds) {
         if (!$this->getContentType($contentTypeSlug)) {
             // sprintf('Attempt to modify invalid ContentType: %s', $contentTypeSlug);
             continue;
         } else {
             $this->app['storage.request.modify']->action($contentTypeSlug, $recordIds);
         }
     }
     $referer = Request::create($request->server->get('HTTP_REFERER'));
     $taxonomy = null;
     foreach (array_keys($this->getOption('taxonomy', [])) as $taxonomyKey) {
         if ($referer->query->get('taxonomy-' . $taxonomyKey)) {
             $taxonomy[$taxonomyKey] = $referer->query->get('taxonomy-' . $taxonomyKey);
         }
     }
     $options = (new ListingOptions())->setOrder($referer->query->get('order'))->setPage($referer->query->get('page_' . $contentType))->setFilter($referer->query->get('filter'))->setTaxonomies($taxonomy);
     $context = ['contenttype' => $this->getContentType($contentType), 'multiplecontent' => $this->app['storage.request.listing']->action($contentType, $options), 'filter' => array_merge((array) $taxonomy, (array) $options->getFilter()), 'permissions' => $this->getContentTypeUserPermissions($contentType, $this->users()->getCurrentUser())];
     return $this->render('@bolt/async/record_list.twig', ['context' => $context]);
 }
All Usage Examples Of Symfony\Component\HttpFoundation\Request::create