Overview
  • Namespace
  • Class

Namespaces

  • Slim
    • Exception
    • Handlers
      • Strategies
    • Http
    • Interfaces
      • Http

Classes

  • Body
  • Cookies
  • Environment
  • Headers
  • Message
  • Request
  • RequestBody
  • Response
  • Stream
  • UploadedFile
  • Uri

Class Request

Request

This class represents an HTTP request. It manages the request method, URI, headers, cookies, and body according to the PSR-7 standard.

Slim\Http\Message implements Psr\Http\Message\MessageInterface
Extended by Slim\Http\Request implements Psr\Http\Message\ServerRequestInterface
Namespace: Slim\Http
Link: https://github.com/php-fig/http-message/blob/master/src/MessageInterface.php
Link: https://github.com/php-fig/http-message/blob/master/src/RequestInterface.php
Link: https://github.com/php-fig/http-message/blob/master/src/ServerRequestInterface.php
Located at Http/Request.php

Methods summary

public static static
# createFromEnvironment( Slim\Http\Environment $environment )

Create new HTTP request with data extracted from the application Environment object

Create new HTTP request with data extracted from the application Environment object

Parameters

$environment
The Slim application Environment

Returns

static
public
# __construct( string $method, Psr\Http\Message\UriInterface $uri, Slim\Interfaces\Http\HeadersInterface $headers, array $cookies, array $serverParams, Psr\Http\Message\StreamInterface $body, array $uploadedFiles = [] )

Create new HTTP request.

Create new HTTP request.

Adds a host header when none was provided and a host is defined in uri.

Parameters

$method
The request method
$uri
The request URI object
$headers
The request headers collection
$cookies
The request cookies collection
$serverParams
The server environment variables
$body
The request body object
$uploadedFiles
The request uploadedFiles collection

Throws

Slim\Exception\InvalidMethodException
on invalid HTTP method
public
# __clone( )

This method is applied to the cloned object after PHP performs an initial shallow-copy. This method completes a deep-copy by creating new objects for the cloned object's internal reference pointers.

This method is applied to the cloned object after PHP performs an initial shallow-copy. This method completes a deep-copy by creating new objects for the cloned object's internal reference pointers.

public string
# getMethod( )

Retrieves the HTTP method of the request.

Retrieves the HTTP method of the request.

Returns

string
Returns the request method.
public string
# getOriginalMethod( )

Get the original HTTP method (ignore override).

Get the original HTTP method (ignore override).

Note: This method is not part of the PSR-7 standard.

Returns

string
public static
# withMethod( string $method )

Return an instance with the provided HTTP method.

Return an instance with the provided HTTP method.

While HTTP method names are typically all uppercase characters, HTTP method names are case-sensitive and thus implementations SHOULD NOT modify the given string.

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the changed request method.

Parameters

$method
Case-sensitive method.

Returns

static

Throws

InvalidArgumentException
for invalid HTTP methods.
protected null|string
# filterMethod( null|string $method )

Validate the HTTP method

Validate the HTTP method

Parameters

$method

Returns

null|string

Throws

InvalidArgumentException
on invalid HTTP method.
public boolean
# isMethod( string $method )

Does this request use a given method?

Does this request use a given method?

Note: This method is not part of the PSR-7 standard.

Parameters

$method
HTTP method

Returns

boolean
public boolean
# isGet( )

Is this a GET request?

Is this a GET request?

Note: This method is not part of the PSR-7 standard.

Returns

boolean
public boolean
# isPost( )

Is this a POST request?

Is this a POST request?

Note: This method is not part of the PSR-7 standard.

Returns

boolean
public boolean
# isPut( )

Is this a PUT request?

Is this a PUT request?

Note: This method is not part of the PSR-7 standard.

Returns

boolean
public boolean
# isPatch( )

Is this a PATCH request?

Is this a PATCH request?

Note: This method is not part of the PSR-7 standard.

Returns

boolean
public boolean
# isDelete( )

Is this a DELETE request?

Is this a DELETE request?

Note: This method is not part of the PSR-7 standard.

Returns

boolean
public boolean
# isHead( )

Is this a HEAD request?

Is this a HEAD request?

Note: This method is not part of the PSR-7 standard.

Returns

boolean
public boolean
# isOptions( )

Is this a OPTIONS request?

Is this a OPTIONS request?

Note: This method is not part of the PSR-7 standard.

Returns

boolean
public boolean
# isXhr( )

Is this an XHR request?

Is this an XHR request?

Note: This method is not part of the PSR-7 standard.

Returns

boolean
public string
# getRequestTarget( )

Retrieves the message's request target.

Retrieves the message's request target.

Retrieves the message's request-target either as it will appear (for clients), as it appeared at request (for servers), or as it was specified for the instance (see withRequestTarget()).

In most cases, this will be the origin-form of the composed URI, unless a value was provided to the concrete implementation (see withRequestTarget() below).

If no URI is available, and no request-target has been specifically provided, this method MUST return the string "/".

Returns

string
public static
# withRequestTarget( mixed $requestTarget )

Return an instance with the specific request-target.

Return an instance with the specific request-target.

If the request needs a non-origin-form request-target — e.g., for specifying an absolute-form, authority-form, or asterisk-form — this method may be used to create an instance with the specified request-target, verbatim.

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the changed request target.

Parameters

$requestTarget

Returns

static

Throws

InvalidArgumentException
if the request target is invalid

Link

(for the various request-target forms allowed in request messages)
public Psr\Http\Message\UriInterface
# getUri( )

Retrieves the URI instance.

Retrieves the URI instance.

This method MUST return a UriInterface instance.

Returns

Psr\Http\Message\UriInterface

Returns a UriInterface instance representing the URI of the request.

Link

http://tools.ietf.org/html/rfc3986#section-4.3
public static
# withUri( Psr\Http\Message\UriInterface $uri, boolean $preserveHost = false )

Returns an instance with the provided URI.

Returns an instance with the provided URI.

This method MUST update the Host header of the returned request by default if the URI contains a host component. If the URI does not contain a host component, any pre-existing Host header MUST be carried over to the returned request.

You can opt-in to preserving the original state of the Host header by setting $preserveHost to true. When $preserveHost is set to true, this method interacts with the Host header in the following ways:

  • If the the Host header is missing or empty, and the new URI contains a host component, this method MUST update the Host header in the returned request.
  • If the Host header is missing or empty, and the new URI does not contain a host component, this method MUST NOT update the Host header in the returned request.
  • If a Host header is present and non-empty, this method MUST NOT update the Host header in the returned request.

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the new UriInterface instance.

Parameters

$uri
New request URI to use.
$preserveHost
Preserve the original state of the Host header.

Returns

static

Link

http://tools.ietf.org/html/rfc3986#section-4.3
public string|null
# getContentType( )

Get request content type.

Get request content type.

Note: This method is not part of the PSR-7 standard.

Returns

string|null
The request content type, if known
public string|null
# getMediaType( )

Get request media type, if known.

Get request media type, if known.

Note: This method is not part of the PSR-7 standard.

Returns

string|null
The request media type, minus content-type params
public array
# getMediaTypeParams( )

Get request media type params, if known.

Get request media type params, if known.

Note: This method is not part of the PSR-7 standard.

Returns

array
public string|null
# getContentCharset( )

Get request content character set, if known.

Get request content character set, if known.

Note: This method is not part of the PSR-7 standard.

Returns

string|null
public integer|null
# getContentLength( )

Get request content length, if known.

Get request content length, if known.

Note: This method is not part of the PSR-7 standard.

Returns

integer|null
public array
# getCookieParams( )

Retrieve cookies.

Retrieve cookies.

Retrieves cookies sent by the client to the server.

The data MUST be compatible with the structure of the $_COOKIE superglobal.

Returns

array
public mixed
# getCookieParam( string $key, mixed $default = null )

Fetch cookie value from cookies sent by the client to the server.

Fetch cookie value from cookies sent by the client to the server.

Note: This method is not part of the PSR-7 standard.

Parameters

$key
The attribute name.
$default
Default value to return if the attribute does not exist.

Returns

mixed
public static
# withCookieParams( array $cookies )

Return an instance with the specified cookies.

Return an instance with the specified cookies.

The data IS NOT REQUIRED to come from the $_COOKIE superglobal, but MUST be compatible with the structure of $_COOKIE. Typically, this data will be injected at instantiation.

This method MUST NOT update the related Cookie header of the request instance, nor related values in the server params.

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the updated cookie values.

Parameters

$cookies
Array of key/value pairs representing cookies.

Returns

static
public array
# getQueryParams( )

Retrieve query string arguments.

Retrieve query string arguments.

Retrieves the deserialized query string arguments, if any.

Note: the query params might not be in sync with the URI or server params. If you need to ensure you are only getting the original values, you may need to parse the query string from getUri()->getQuery() or from the QUERY_STRING server param.

Returns

array
public static
# withQueryParams( array $query )

Return an instance with the specified query string arguments.

Return an instance with the specified query string arguments.

These values SHOULD remain immutable over the course of the incoming request. They MAY be injected during instantiation, such as from PHP's $_GET superglobal, or MAY be derived from some other value such as the URI. In cases where the arguments are parsed from the URI, the data MUST be compatible with what PHP's parse_str() would return for purposes of how duplicate query parameters are handled, and how nested sets are handled.

Setting query string arguments MUST NOT change the URI stored by the request, nor the values in the server params.

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the updated query string arguments.

Parameters

$query

Array of query string arguments, typically from $_GET.

Returns

static
public array
# getUploadedFiles( )

Retrieve normalized file upload data.

Retrieve normalized file upload data.

This method returns upload metadata in a normalized tree, with each leaf an instance of Psr\Http\Message\UploadedFileInterface.

These values MAY be prepared from $_FILES or the message body during instantiation, or MAY be injected via withUploadedFiles().

Returns

array

An array tree of UploadedFileInterface instances; an empty array MUST be returned if no data is present.

public static
# withUploadedFiles( array $uploadedFiles )

Create a new instance with the specified uploaded files.

Create a new instance with the specified uploaded files.

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the updated body parameters.

Parameters

$uploadedFiles
An array tree of UploadedFileInterface instances.

Returns

static

Throws

InvalidArgumentException
if an invalid structure is provided.
public array
# getServerParams( )

Retrieve server parameters.

Retrieve server parameters.

Retrieves data related to the incoming request environment, typically derived from PHP's $_SERVER superglobal. The data IS NOT REQUIRED to originate from $_SERVER.

Returns

array
public mixed
# getServerParam( string $key, mixed $default = null )

Retrieve a server parameter.

Retrieve a server parameter.

Note: This method is not part of the PSR-7 standard.

Parameters

$key
$default

Returns

mixed
public array
# getAttributes( )

Retrieve attributes derived from the request.

Retrieve attributes derived from the request.

The request "attributes" may be used to allow injection of any parameters derived from the request: e.g., the results of path match operations; the results of decrypting cookies; the results of deserializing non-form-encoded message bodies; etc. Attributes will be application and request specific, and CAN be mutable.

Returns

array
Attributes derived from the request.
public mixed
# getAttribute( string $name, mixed $default = null )

Retrieve a single derived request attribute.

Retrieve a single derived request attribute.

Retrieves a single derived request attribute as described in getAttributes(). If the attribute has not been previously set, returns the default value as provided.

This method obviates the need for a hasAttribute() method, as it allows specifying a default value to return if the attribute is not found.

Parameters

$name
The attribute name.
$default
Default value to return if the attribute does not exist.

Returns

mixed

See

Slim\Http\Request::getAttributes()
public static
# withAttribute( string $name, mixed $value )

Return an instance with the specified derived request attribute.

Return an instance with the specified derived request attribute.

This method allows setting a single derived request attribute as described in getAttributes().

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the updated attribute.

Parameters

$name
The attribute name.
$value
The value of the attribute.

Returns

static

See

Slim\Http\Request::getAttributes()
public static
# withAttributes( array $attributes )

Create a new instance with the specified derived request attributes.

Create a new instance with the specified derived request attributes.

Note: This method is not part of the PSR-7 standard.

This method allows setting all new derived request attributes as described in getAttributes().

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return a new instance that has the updated attributes.

Parameters

$attributes
New attributes

Returns

static
public static
# withoutAttribute( string $name )

Return an instance that removes the specified derived request attribute.

Return an instance that removes the specified derived request attribute.

This method allows removing a single derived request attribute as described in getAttributes().

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that removes the attribute.

Parameters

$name
The attribute name.

Returns

static

See

Slim\Http\Request::getAttributes()
public null|array|object
# getParsedBody( )

Retrieve any parameters provided in the request body.

Retrieve any parameters provided in the request body.

If the request Content-Type is either application/x-www-form-urlencoded or multipart/form-data, and the request method is POST, this method MUST return the contents of $_POST.

Otherwise, this method may return any results of deserializing the request body content; as parsing returns structured content, the potential types MUST be arrays or objects only. A null value indicates the absence of body content.

Returns

null|array|object

The deserialized body parameters, if any. These will typically be an array or object.

Throws

RuntimeException
if the request body media type parser returns an invalid value
public static
# withParsedBody( null|array|object $data )

Return an instance with the specified body parameters.

Return an instance with the specified body parameters.

These MAY be injected during instantiation.

If the request Content-Type is either application/x-www-form-urlencoded or multipart/form-data, and the request method is POST, use this method ONLY to inject the contents of $_POST.

The data IS NOT REQUIRED to come from $_POST, but MUST be the results of deserializing the request body content. Deserialization/parsing returns structured data, and, as such, this method ONLY accepts arrays or objects, or a null value if nothing was available to parse.

As an example, if content negotiation determines that the request data is a JSON payload, this method could be used to create a request instance with the deserialized parameters.

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the updated body parameters.

Parameters

$data

The deserialized body data. This will typically be in an array or object.

Returns

static

Throws

InvalidArgumentException

if an unsupported argument type is provided.

public
# reparseBody( )

Force Body to be parsed again.

Force Body to be parsed again.

Note: This method is not part of the PSR-7 standard.

Returns


$this
public
# registerMediaTypeParser( string $mediaType, callable $callable )

Register media type parser.

Register media type parser.

Note: This method is not part of the PSR-7 standard.

Parameters

$mediaType

A HTTP media type (excluding content-type params).

$callable

A callable that returns parsed contents for media type.

public mixed
# getParam( string $key, string $default = null )

Fetch request parameter value from body or query string (in that order).

Fetch request parameter value from body or query string (in that order).

Note: This method is not part of the PSR-7 standard.

Parameters

$key
The parameter key.
$default
The default value.

Returns

mixed
The parameter value.
public mixed
# getParsedBodyParam( string $key, mixed $default = null )

Fetch parameter value from request body.

Fetch parameter value from request body.

Note: This method is not part of the PSR-7 standard.

Parameters

$key
$default

Returns

mixed
public mixed
# getQueryParam( string $key, mixed $default = null )

Fetch parameter value from query string.

Fetch parameter value from query string.

Note: This method is not part of the PSR-7 standard.

Parameters

$key
$default

Returns

mixed
public array
# getParams( )

Fetch associative array of body and query string parameters.

Fetch associative array of body and query string parameters.

Note: This method is not part of the PSR-7 standard.

Returns

array

Methods inherited from Slim\Http\Message

__set(), getBody(), getHeader(), getHeaderLine(), getHeaders(), getProtocolVersion(), hasHeader(), withAddedHeader(), withBody(), withHeader(), withProtocolVersion(), withoutHeader()

Properties summary

protected string $method

The request method

The request method

#
protected string $originalMethod

The original request method (ignoring override)

The original request method (ignoring override)

#
protected Psr\Http\Message\UriInterface $uri

The request URI object

The request URI object

#
protected string $requestTarget

The request URI target (path + query string)

The request URI target (path + query string)

#
protected array $queryParams

The request query string params

The request query string params

#
protected array $cookies

The request cookies

The request cookies

#
protected array $serverParams

The server environment variables at the time the request was created.

The server environment variables at the time the request was created.

#
protected Slim\Collection $attributes

The request attributes (route segment names and values)

The request attributes (route segment names and values)

#
protected null|array|object $bodyParsed

The request body parsed (if possible) into a PHP array or object

The request body parsed (if possible) into a PHP array or object

# false
protected callable[] $bodyParsers

List of request body parsers (e.g., url-encoded, JSON, XML, multipart)

List of request body parsers (e.g., url-encoded, JSON, XML, multipart)

# []
protected Psr\Http\Message\UploadedFileInterface $uploadedFiles

List of uploaded files

List of uploaded files

#
protected string[] $validMethods

Valid request methods

Valid request methods

Deprecated

# [ 'CONNECT' => 1, 'DELETE' => 1, 'GET' => 1, 'HEAD' => 1, 'OPTIONS' => 1, 'PATCH' => 1, 'POST' => 1, 'PUT' => 1, 'TRACE' => 1, ]

Properties inherited from Slim\Http\Message

$body, $headers, $protocolVersion, $validProtocolVersions

API documentation generated by ApiGen