Fr3nch13/CakePHP Utilities

ServerRequest
in package
implements ServerRequestInterface uses RequestTrait

Server-side HTTP request

Extends the Request definition to add methods for accessing incoming data, specifically server parameters, cookies, matched path parameters, query string arguments, body parameters, and upload file information.

"Attributes" are discovered via decomposing the request (and usually specifically the URI path), and typically will be injected by the application.

Requests are considered immutable; all methods that might change state are implemented such that they retain the internal state of the current message and return a new instance that contains the changed state.

Table of Contents

Interfaces

ServerRequestInterface
Representation of an incoming, server-side HTTP request.

Properties

$attributes  : array<string|int, mixed>
$cookieParams  : array<string|int, mixed>
$method  : string
$parsedBody  : mixed
$queryParams  : array<string|int, mixed>
$requestTarget  : null|string
The request-target, if it has been provided or calculated.
$serverParams  : array<string|int, mixed>
$uploadedFiles  : array<string|int, mixed>
$uri  : UriInterface

Methods

__construct()  : mixed
getAttribute()  : mixed
Retrieve a single derived request attribute.
getAttributes()  : array<string|int, mixed>
Retrieve attributes derived from the request.
getCookieParams()  : array<string|int, mixed>
Retrieve cookies.
getMethod()  : string
Retrieves the HTTP method of the request.
getParsedBody()  : null|array<string|int, mixed>|object
Retrieve any parameters provided in the request body.
getQueryParams()  : array<string|int, mixed>
Retrieve query string arguments.
getRequestTarget()  : string
Retrieves the message's request target.
getServerParams()  : array<string|int, mixed>
Retrieve server parameters.
getUploadedFiles()  : array<string|int, mixed>
Retrieve normalized file upload data.
getUri()  : UriInterface
Retrieves the URI instance.
withAttribute()  : static
Return an instance with the specified derived request attribute.
withCookieParams()  : static
Return an instance with the specified cookies.
withMethod()  : static
Return an instance with the provided HTTP method.
withoutAttribute()  : static
Return an instance that removes the specified derived request attribute.
withParsedBody()  : static
Return an instance with the specified body parameters.
withQueryParams()  : static
Return an instance with the specified query string arguments.
withRequestTarget()  : static
Create a new instance with a specific request-target.
withUploadedFiles()  : static
Create a new instance with the specified uploaded files.
withUri()  : static
Returns an instance with the provided URI.
createUri()  : UriInterface
Create and return a URI instance.
getHostFromUri()  : string
Retrieve the host from the URI instance
initialize()  : void
Initialize request state.
setMethod()  : void
Set and validate the HTTP method
validateUploadedFiles()  : void
Recursively validate the structure in an uploaded files array.

Properties

$attributes

private array<string|int, mixed> $attributes = []

$cookieParams

private array<string|int, mixed> $cookieParams = []

$queryParams

private array<string|int, mixed> $queryParams = []

$requestTarget

The request-target, if it has been provided or calculated.

private null|string $requestTarget

$serverParams

private array<string|int, mixed> $serverParams = []

$uploadedFiles

private array<string|int, mixed> $uploadedFiles

Methods

__construct()

public __construct([array<string|int, mixed> $serverParams = [] ][, array<string|int, mixed> $uploadedFiles = [] ][, null|string|UriInterface $uri = null ][, null|string $method = null ][, string|resource|StreamInterface $body = 'php://input' ][, array<string|int, mixed> $headers = [] ][, array<string|int, mixed> $cookieParams = [] ][, array<string|int, mixed> $queryParams = [] ][, null|array<string|int, mixed>|object $parsedBody = null ][, string $protocol = '1.1' ]) : mixed
Parameters
$serverParams : array<string|int, mixed> = []

Server parameters, typically from $_SERVER

$uploadedFiles : array<string|int, mixed> = []

Upload file information, a tree of UploadedFiles

$uri : null|string|UriInterface = null

URI for the request, if any.

$method : null|string = null

HTTP method for the request, if any.

$body : string|resource|StreamInterface = 'php://input'

Message body, if any.

$headers : array<string|int, mixed> = []

Headers for the message, if any.

$cookieParams : array<string|int, mixed> = []

Cookies for the message, if any.

$queryParams : array<string|int, mixed> = []

Query params for the message, if any.

$parsedBody : null|array<string|int, mixed>|object = null

The deserialized body parameters, if any.

$protocol : string = '1.1'

HTTP protocol version.

Tags
throws
InvalidArgumentException

For any invalid value.

getAttribute()

Retrieve a single derived request attribute.

public getAttribute(mixed $attribute[, mixed $default = null ]) : mixed
Parameters
$attribute : mixed
$default : mixed = null

Default value to return if the attribute does not exist.

getAttributes()

Retrieve attributes derived from the request.

public getAttributes() : array<string|int, mixed>
Return values
array<string|int, mixed>

Attributes derived from the request.

getCookieParams()

Retrieve cookies.

public getCookieParams() : array<string|int, mixed>
Return values
array<string|int, mixed>

getMethod()

Retrieves the HTTP method of the request.

public getMethod() : string
Return values
string

Returns the request method.

getParsedBody()

Retrieve any parameters provided in the request body.

public getParsedBody() : null|array<string|int, mixed>|object
Return values
null|array<string|int, mixed>|object

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

getQueryParams()

Retrieve query string arguments.

public getQueryParams() : array<string|int, mixed>
Return values
array<string|int, mixed>

getRequestTarget()

Retrieves the message's request target.

public getRequestTarget() : string

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 "/".

Return values
string

getServerParams()

Retrieve server parameters.

public getServerParams() : array<string|int, mixed>
Return values
array<string|int, mixed>

getUploadedFiles()

Retrieve normalized file upload data.

public getUploadedFiles() : array<string|int, mixed>
Return values
array<string|int, mixed>

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

withAttribute()

Return an instance with the specified derived request attribute.

public withAttribute(mixed $attribute, mixed $value) : static
Parameters
$attribute : mixed
$value : mixed

The value of the attribute.

Return values
static

withCookieParams()

Return an instance with the specified cookies.

public withCookieParams(array<string|int, mixed> $cookies) : static
Parameters
$cookies : array<string|int, mixed>

Array of key/value pairs representing cookies.

Return values
static

withMethod()

Return an instance with the provided HTTP method.

public withMethod(string $method) : static

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 : string

Case-insensitive method.

Tags
throws
InvalidArgumentException

For invalid HTTP methods.

Return values
static

withoutAttribute()

Return an instance that removes the specified derived request attribute.

public withoutAttribute(mixed $name) : static
Parameters
$name : mixed

The attribute name.

Return values
static

withParsedBody()

Return an instance with the specified body parameters.

public withParsedBody(mixed $data) : static
Parameters
$data : mixed

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

Return values
static

withQueryParams()

Return an instance with the specified query string arguments.

public withQueryParams(array<string|int, mixed> $query) : static
Parameters
$query : array<string|int, mixed>

Array of query string arguments, typically from $_GET.

Return values
static

withRequestTarget()

Create a new instance with a specific request-target.

public withRequestTarget(string $requestTarget) : static

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 a new instance that has the changed request target.

Parameters
$requestTarget : string
Tags
link

(for the various request-target forms allowed in request messages)

throws
InvalidArgumentException

If the request target is invalid.

Return values
static

withUploadedFiles()

Create a new instance with the specified uploaded files.

public withUploadedFiles(array<string|int, mixed> $uploadedFiles) : static
Parameters
$uploadedFiles : array<string|int, mixed>

An array tree of UploadedFileInterface instances.

Return values
static

withUri()

Returns an instance with the provided URI.

public withUri(UriInterface $uri[, bool $preserveHost = false ]) : static

This method will 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 will 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, the returned request will not update the Host header of the returned message -- even if the message contains no Host header. This means that a call to getHeader('Host') on the original request MUST equal the return value of a call to getHeader('Host') on 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 : UriInterface

New request URI to use.

$preserveHost : bool = false

Preserve the original state of the Host header.

Tags
link
http://tools.ietf.org/html/rfc3986#section-4.3
Return values
static

createUri()

Create and return a URI instance.

private createUri(null|string|UriInterface $uri) : UriInterface

If $uri is a already a UriInterface instance, returns it.

If $uri is a string, passes it to the Uri constructor to return an instance.

If $uri is null, creates and returns an empty Uri` instance.

Otherwise, it raises an exception.

Parameters
$uri : null|string|UriInterface
Tags
throws
InvalidArgumentException
Return values
UriInterface

getHostFromUri()

Retrieve the host from the URI instance

private getHostFromUri() : string
Return values
string

initialize()

Initialize request state.

private initialize([null|string|UriInterface $uri = null ][, null|string $method = null ][, string|resource|StreamInterface $body = 'php://memory' ][, array<string|int, mixed> $headers = [] ]) : void

Used by constructors.

Parameters
$uri : null|string|UriInterface = null

URI for the request, if any.

$method : null|string = null

HTTP method for the request, if any.

$body : string|resource|StreamInterface = 'php://memory'

Message body, if any.

$headers : array<string|int, mixed> = []

Headers for the message, if any.

Tags
throws
InvalidArgumentException

For any invalid value.

setMethod()

Set and validate the HTTP method

private setMethod(string $method) : void
Parameters
$method : string
Tags
throws
InvalidArgumentException

On invalid HTTP method.

validateUploadedFiles()

Recursively validate the structure in an uploaded files array.

private validateUploadedFiles(array<string|int, mixed> $uploadedFiles) : void
Parameters
$uploadedFiles : array<string|int, mixed>
Tags
throws
InvalidArgumentException

If any leaf is not an UploadedFileInterface instance.


        
On this page

Search results