Fork me on GitHub

Documentation

HTTP

The HTTP component wraps PHP's request super-globals — $_GET, $_POST, $_FILES, $_COOKIE, and $_SERVER — into an object-oriented interface. Each bag is exposed as a read-only property hook on the Request object, and the Http container holds the request together with convenience helpers.

In a controller (AbstractController), the current request is available directly as $this->request, or via the HTTP container as $this->http->request.

Parameter Bags

The Request provides five Parameter bags as public read-only properties. Each bag is a Parameter value object that implements Countable and IteratorAggregate:

Property Source Super-global Fallback
$request->query 'get' key of the injected array $_GET
$request->post 'post' key of the injected array $_POST
$request->cookie 'cookie' key of the injected array $_COOKIE
$request->file 'file' key of the injected array $_FILES
$request->server 'server' key of the injected array $_SERVER

Data Access

Data is injected at construction and falls back to the super-globals when a key is omitted, so the component is easy to test and does not read global state once you pass it an explicit request array.

GET Data (Query String)

The $request->query bag returns query string data from the 'get' key (or $_GET):

    

    // In a controller action:
    $page = $this->request->query->get('page');         // null if absent
    $page = $this->request->query->get('page', 1);      // default to 1
    
                

POST Data

The $request->post bag returns POST data from the 'post' key (or $_POST):

    

    $name = $this->request->post->get('name');            // null if absent
    $name = $this->request->post->get('name', 'guest');   // default to 'guest'
    
                

The $request->cookie bag returns cookie data from the 'cookie' key (or $_COOKIE):

    

    $sessionId = $this->request->cookie->get('PHPSESSID');
    
                

File Data

The $request->file bag returns file data from the 'file' key (or $_FILES):

    

    $upload = $this->request->file->get('upload');
    
                

Server Data

The $request->server bag returns server data from the 'server' key (or $_SERVER):

    

    $method = $this->request->server->get('REQUEST_METHOD');
    $agent  = $this->request->server->get('HTTP_USER_AGENT');
    
                

Parameter Methods

Every Parameter bag supports the following methods:

Method Description
get($key, $default = null) Gets a parameter value, or $default if absent
has($key) Returns true if the key exists, false otherwise
add(array $parameter) Merges an associative array into the bag
remove($key) Removes a parameter by key
all() Returns all parameters as an associative array
count() Returns the number of parameters (implements Countable)
getIterator() Returns an ArrayIterator (implements IteratorAggregate)
    

    // Check if a key exists
    if ($this->request->query->has('token')) {
        // ...
    }

    // Iterate over server variables
    foreach ($this->request->server as $key => $value) {
        // ...
    }
    
                

Request Helpers

The Request object also provides several convenience methods:

Ajax Detection

isAjax() returns true when the X-Requested-With header equals XMLHttpRequest:

    

    if ($this->request->isAjax()) {
        // Return JSON instead of rendering the layout
    }
    
                

Secure Request Detection

isSecure() returns true when the request was made over HTTPS:

    

    if ($this->request->isSecure()) {
        // Enforce HTTPS
    }
    
                

Base URL

baseUrl() returns the full base URL (protocol, host, and path up to the front controller directory):

    

    $baseUrl = $this->request->baseUrl();
    // e.g. "https://example.com/my-app"
    
                

The HTTP Container

The Http object wraps the request and is available on the application as $app->http and on controllers as $this->http. In the rare case you need to access it directly:

    

    $request = $this->http->request;  // same as $this->request