Request
extends AbstractRequest
in package
implements
RequestInterface
Class for handling various variables from the server.
Tags
Interfaces, Classes and Traits
- RequestInterface
- Representation of an outgoing, client-side request.
Table of Contents
- $headers : array<string|int, mixed>
- $method : string|null
- $protocol : string
- $requestTarget : string|null
- $stream : StreamInterface
- $uri : Uri|null
- $attributes : array<string|int, mixed>
- $cookieParams : array<string|int, mixed>
- $_COOKIE
- $parsedBody : array<string|int, mixed>
- $_POST
- $queryParams : array<string|int, mixed>
- $_GET
- $serverParams : array<string|int, mixed>
- $_SERVER
- $uploadedFiles : Files
- __construct() : mixed
- Initializes the class.
- cookie() : InputItem|Inputs
- Reads a value from the COOKIE variable.
- createToken() : string
- Generates a token.
- createUriWithGet() : Uri
- Create a class from a link and merge values from $_GET
- createUriWithGlobals() : Uri
- Create a class from a link and merge values from $_GET and $_POST
- createUriWithPost() : Uri
- Create a class from a link and merge values from $_POST
- filterRequestKey() : array<string|int, mixed>
- Filters the keys of the requests.
- get() : InputItem|Inputs
- Reads a value from the GET variable.
- getAcceptableLanguages() : array<string|int, mixed>
- Returns the list of acceptable languages from the HTTP header.
- getAttribute() : mixed
- Retrieves the attribute with the given name.
- getAttributes() : array<string|int, mixed>
- Retrieves the attributes.
- getBody() : StreamInterface
- Read the stream.
- getClientIp() : array<string|int, mixed>|null
- Retrieves the parsed body parameters, if any.
- getCookieParams() : array<string|int, mixed>
- Get values from the $_COOKIE variable.
- getHeader() : array<string|int, string>
- Get the specified header as an array.
- getHeaderLine() : string
- Get the specified header as a string.
- getHeaders() : array<string|int, mixed>
- Get all the headers.
- getMethod() : string
- Get the HTTP method
- getParsedBody() : null|array<string|int, mixed>|object
- Get values from the $_POST variable.
- getProtocolVersion() : string
- Get the protocol version of the message.
- getQueryParams() : null|array<string|int, mixed>|object
- Get values from the $_GET variable.
- getRequestTarget() : string
- Get the request target
- getServerParams() : array<string|int, mixed>
- Get values from the $_SERVER variable.
- getUploadedFiles() : Files
- Get uploaded files from $_FILES.
- getUri() : Uri
- Get the URI
- globals() : InputItem|Inputs
- Read values from the GLOBALS variables ($_POST, $_GET, $_SESSION, $_COOKIE) according to the specified keys.
- hasHeader() : bool
- Check if a header exists.
- initSession() : bool
- Initialize session.
- isAjax() : bool
- Check if the request is made via Ajax.
- isReferer() : bool
- Check if the referer is from the same website.
- isSafe() : bool
- Check the token from the form and validate the referer.
- map() : mixed
- Merge arrays $_GET and $_POST into a single data array
- post() : InputItem|Inputs
- Read a value from the $_POST variable.
- removeToken() : mixed
- Remove the token.
- request() : InputItem|Inputs
- Read a value from the $_POST, $_GET, $_COOKIE (optional) variables in order.
- server() : mixed
- Read a value from the $_SERVER variable.
- session() : InputItem|Inputs
- Read a value from the $_SESSION variable.
- setSession() : static
- Set the value of $_SESSION variable.
- withAddedHeader() : static
- Add a new header.
- withAttribute() : static
- Set the value of attributes.
- withBody() : static
- Set the message body.
- withCookieParams() : static
- Set the value of cookieParams.
- withHeader() : static
- Set a header, replacing any existing values.
- withHeaders() : static
- Set multiple headers, replacing any existing values.
- withMethod() : static
- Set the HTTP method
- withoutAttribute() : static
- Removes attributes.
- withoutHeader() : static
- Remove a header.
- withParsedBody() : static
- Set the value of parsedBody.
- withProtocolVersion() : static
- Set the protocol version of the message.
- withQueryParams() : static
- Set the value of queryParams.
- withRequestTarget() : static
- Set the request target
- withUploadedFiles() : static
- Set the value of uploadedFiles.
- withUri() : static
- Set the URI
- filterHeader() : mixed
- Check the validity of a header.
- getRequestHeaders() : array<string|int, mixed>
- Function to retrieve HTTP headers.
- createInputItem() : InputItem|Inputs
- Reads a value from the $source.
Properties
$headers
protected
array<string|int, mixed>
$headers
= []
The headers of the message.
$method
protected
string|null
$method
= null
The HTTP method
$protocol
protected
string
$protocol
= '1.1'
The protocol version of the message.
$requestTarget
protected
string|null
$requestTarget
The request target
$stream
protected
StreamInterface
$stream
The body of the message.
$uri
protected
Uri|null
$uri
The URI
$attributes
private
array<string|int, mixed>
$attributes
= []
$cookieParams
$_COOKIE
private
array<string|int, mixed>
$cookieParams
$parsedBody
$_POST
private
array<string|int, mixed>
$parsedBody
$queryParams
$_GET
private
array<string|int, mixed>
$queryParams
$serverParams
$_SERVER
private
array<string|int, mixed>
$serverParams
$uploadedFiles
private
Files
$uploadedFiles
Methods
__construct()
Initializes the class.
public
__construct([bool $withHeader = false ]) : mixed
Parameters
- $withHeader : bool = false
-
Whether to include the HTTP header or not. Default is false.
Return values
mixed —cookie()
Reads a value from the COOKIE variable.
public
cookie(string $name[, mixed $default = '' ]) : InputItem|Inputs
Returns an InputItem or a collection of InputItem.
Parameters
- $name : string
-
The name of the variable.
- $default : mixed = ''
-
The default value if the variable is not found.
Return values
InputItem|Inputs —createToken()
Generates a token.
public
createToken() : string
Return values
string —The generated token.
createUriWithGet()
Create a class from a link and merge values from $_GET
public
static createUriWithGet([string $uri = 'index.php' ][, array<string|int, mixed> $exclude = [] ]) : Uri
Parameters
- $uri : string = 'index.php'
-
The URI, default is 'index.php'
- $exclude : array<string|int, mixed> = []
-
An array of keys from $_GET to exclude from the URL
Return values
Uri —createUriWithGlobals()
Create a class from a link and merge values from $_GET and $_POST
public
createUriWithGlobals([string $uri = 'index.php' ][, array<string|int, mixed> $exclude = [] ]) : Uri
Parameters
- $uri : string = 'index.php'
-
The URI, default is 'index.php'
- $exclude : array<string|int, mixed> = []
-
An array of keys from $_GET and $_POST to exclude from the URL
Return values
Uri —createUriWithPost()
Create a class from a link and merge values from $_POST
public
static createUriWithPost([string $uri = 'index.php' ][, array<string|int, mixed> $exclude = [] ]) : Uri
Parameters
- $uri : string = 'index.php'
-
The URI, default is 'index.php'
- $exclude : array<string|int, mixed> = []
-
An array of keys from $_POST to exclude from the URL
Return values
Uri —filterRequestKey()
Filters the keys of the requests.
public
static filterRequestKey(array<string|int, mixed> $source) : array<string|int, mixed>
Parameters
- $source : array<string|int, mixed>
-
The source array.
Return values
array<string|int, mixed> —The filtered array.
get()
Reads a value from the GET variable.
public
get(string $name[, mixed $default = null ][, string $cookie = null ]) : InputItem|Inputs
Returns an InputItem or a collection of InputItem.
Parameters
- $name : string
-
The name of the variable.
- $default : mixed = null
-
The default value if the variable is not found.
- $cookie : string = null
-
null (default) to not read from the cookie, string specifying the cookie name to read from.
Return values
InputItem|Inputs —getAcceptableLanguages()
Returns the list of acceptable languages from the HTTP header.
public
getAcceptableLanguages() : array<string|int, mixed>
Return values
array<string|int, mixed> —The list of acceptable languages.
getAttribute()
Retrieves the attribute with the given name.
public
getAttribute(string $name[, mixed $default = null ]) : mixed
Parameters
- $name : string
-
The attribute name.
- $default : mixed = null
-
The default value if the attribute is not found.
Return values
mixed —The attribute value.
getAttributes()
Retrieves the attributes.
public
getAttributes() : array<string|int, mixed>
Return values
array<string|int, mixed> —The attributes.
getBody()
Read the stream.
public
getBody() : StreamInterface
Return values
StreamInterface —getClientIp()
Retrieves the parsed body parameters, if any.
public
getClientIp() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null —The parsed body parameters.
getCookieParams()
Get values from the $_COOKIE variable.
public
getCookieParams() : array<string|int, mixed>
Return values
array<string|int, mixed> —getHeader()
Get the specified header as an array.
public
getHeader(string $name) : array<string|int, string>
Parameters
- $name : string
-
The name of the header.
Return values
array<string|int, string> —The values of the header as an array, or an empty array if not found.
getHeaderLine()
Get the specified header as a string.
public
getHeaderLine(string $name) : string
Parameters
- $name : string
-
The name of the header.
Return values
string —The concatenated values of the header, separated by commas, or an empty string if not found.
getHeaders()
Get all the headers.
public
getHeaders() : array<string|int, mixed>
Return values
array<string|int, mixed> —The headers of the message.
getMethod()
Get the HTTP method
public
getMethod() : string
Return values
string —The request method
getParsedBody()
Get values from the $_POST variable.
public
getParsedBody() : null|array<string|int, mixed>|object
Return values
null|array<string|int, mixed>|object —getProtocolVersion()
Get the protocol version of the message.
public
getProtocolVersion() : string
Return values
string —The protocol version.
getQueryParams()
Get values from the $_GET variable.
public
getQueryParams() : null|array<string|int, mixed>|object
Return values
null|array<string|int, mixed>|object —getRequestTarget()
Get the request target
public
getRequestTarget() : string
Return values
string —The request target
getServerParams()
Get values from the $_SERVER variable.
public
getServerParams() : array<string|int, mixed>
Return values
array<string|int, mixed> —getUploadedFiles()
Get uploaded files from $_FILES.
public
getUploadedFiles() : Files
Return values
Files —getUri()
Get the URI
public
getUri() : Uri
Return values
Uri —globals()
Read values from the GLOBALS variables ($_POST, $_GET, $_SESSION, $_COOKIE) according to the specified keys.
public
globals(array<string|int, mixed> $keys, string $name[, mixed $default = null ][, string $cookie_or_session_name = null ]) : InputItem|Inputs
For example, if the keys are ['POST', 'GET'], it will read from $_POST first and if not found, it will read from $_GET. If the value is still not found, it will return the default value.
Parameters
- $keys : array<string|int, mixed>
-
The names of the variables to read (uppercase), e.g., ['POST', 'GET'].
- $name : string
-
The name of the variable.
- $default : mixed = null
-
The default value if the variable is not found.
- $cookie_or_session_name : string = null
-
The name of the cookie or session if it's different from $name.
Return values
InputItem|Inputs —hasHeader()
Check if a header exists.
public
hasHeader(string $name) : bool
Parameters
- $name : string
-
The name of the header.
Return values
bool —True if the header exists, false otherwise.
initSession()
Initialize session.
public
initSession() : bool
Return values
bool —isAjax()
Check if the request is made via Ajax.
public
isAjax() : bool
Return true if the request is an Ajax request (XMLHttpRequest).
Return values
bool —isReferer()
Check if the referer is from the same website.
public
isReferer() : bool
Return true if the referer is from the same website.
Return values
bool —isSafe()
Check the token from the form and validate the referer.
public
isSafe() : bool
Only accepts values from $_POST. This function must be called after initSession() every time. The token has a lifetime defined by TOKEN_LIMIT. Return true if the token is valid and not expired.
Return values
bool —map()
Merge arrays $_GET and $_POST into a single data array
public
static map(array<string|int, mixed> &$result, array<string|int, mixed> $array[, array<string|int, mixed> $exclude = [] ]) : mixed
Parameters
- $result : array<string|int, mixed>
-
A variable to store the result for further use
- $array : array<string|int, mixed>
-
The array to merge, e.g., $_GET or $_POST
- $exclude : array<string|int, mixed> = []
-
A list of keys from the array that should not be included in the result
Return values
mixed —post()
Read a value from the $_POST variable.
public
post(string $name[, mixed $default = null ][, string|null $cookie = null ]) : InputItem|Inputs
If not found, return the $default value. Return an InputItem or an array of InputItem.
Parameters
- $name : string
-
The variable name.
- $default : mixed = null
-
The default value if the variable is not found.
- $cookie : string|null = null
-
null (default) to not read from cookie, string the name of the cookie to read from.
Return values
InputItem|Inputs —removeToken()
Remove the token.
public
removeToken() : mixed
Return values
mixed —request()
Read a value from the $_POST, $_GET, $_COOKIE (optional) variables in order.
public
request(string $name[, mixed $default = null ][, string|null $cookie = null ]) : InputItem|Inputs
Return the first item found, if not found, return the $default value. Return an InputItem or an array of InputItem.
Parameters
- $name : string
-
The variable name.
- $default : mixed = null
-
The default value if the variable is not found.
- $cookie : string|null = null
-
null (default) to not read from cookie, string the name of the cookie to read from.
Return values
InputItem|Inputs —server()
Read a value from the $_SERVER variable.
public
server(string $name[, mixed $default = null ]) : mixed
If not found, return the $default value.
Parameters
- $name : string
-
The variable name.
- $default : mixed = null
-
The default value if the variable is not found.
Return values
mixed —session()
Read a value from the $_SESSION variable.
public
session(string $name[, mixed $default = null ]) : InputItem|Inputs
If not found, return the $default value. Return an InputItem or a Collection of InputItem.
Parameters
- $name : string
-
The variable name.
- $default : mixed = null
-
The default value if the variable is not found.
Return values
InputItem|Inputs —setSession()
Set the value of $_SESSION variable.
public
setSession(string $name, mixed $value) : static
Parameters
- $name : string
-
The variable name.
- $value : mixed
-
The value of the variable.
Return values
static —withAddedHeader()
Add a new header.
public
withAddedHeader(string $name, string|array<string|int, string> $value) : static
Parameters
- $name : string
-
The name of the header.
- $value : string|array<string|int, string>
-
The value(s) of the header as a string or an array of strings.
Tags
Return values
static —withAttribute()
Set the value of attributes.
public
withAttribute(string $name, mixed $value) : static
Parameters
- $name : string
-
The name of the attribute.
- $value : mixed
-
The value of the attribute.
Return values
static —withBody()
Set the message body.
public
withBody(StreamInterface $body) : static
Parameters
- $body : StreamInterface
-
The message body.
Return values
static —withCookieParams()
Set the value of cookieParams.
public
withCookieParams(array<string|int, mixed> $cookies) : static
Parameters
- $cookies : array<string|int, mixed>
-
The cookie parameters.
Return values
static —withHeader()
Set a header, replacing any existing values.
public
withHeader(string $name, string|array<string|int, string> $value) : static
Parameters
- $name : string
-
The name of the header.
- $value : string|array<string|int, string>
-
The value(s) of the header as a string or an array of strings.
Tags
Return values
static —withHeaders()
Set multiple headers, replacing any existing values.
public
withHeaders(array<string|int, mixed> $headers) : static
Parameters
- $headers : array<string|int, mixed>
-
An array of headers in the format: array($key => $value, $key => $value...)
Tags
Return values
static —withMethod()
Set the HTTP method
public
withMethod(string $method) : static
Parameters
- $method : string
-
The HTTP method
Return values
static —withoutAttribute()
Removes attributes.
public
withoutAttribute(string|array<string|int, mixed> $names) : static
Parameters
- $names : string|array<string|int, mixed>
-
The names of the attributes to remove.
Return values
static —withoutHeader()
Remove a header.
public
withoutHeader(string $name) : static
Parameters
- $name : string
-
The name of the header to remove.
Return values
static —withParsedBody()
Set the value of parsedBody.
public
withParsedBody(mixed $data) : static
Parameters
- $data : mixed
-
The parsed body data.
Return values
static —withProtocolVersion()
Set the protocol version of the message.
public
withProtocolVersion(string $version) : static
Parameters
- $version : string
-
The protocol version (e.g., 1.1, 1.0).
Return values
static —withQueryParams()
Set the value of queryParams.
public
withQueryParams(array<string|int, mixed> $query) : static
Parameters
- $query : array<string|int, mixed>
-
The query parameters.
Return values
static —withRequestTarget()
Set the request target
public
withRequestTarget(mixed $requestTarget) : static
Parameters
- $requestTarget : mixed
-
The request target
Return values
static —withUploadedFiles()
Set the value of uploadedFiles.
public
withUploadedFiles(array<string|int, mixed> $uploadedFiles) : static
Parameters
- $uploadedFiles : array<string|int, mixed>
-
The uploaded files.
Return values
static —withUri()
Set the URI
public
withUri(UriInterface $uri[, bool $preserveHost = false ]) : static
Parameters
- $uri : UriInterface
-
The URI
- $preserveHost : bool = false
-
Whether to preserve the host header
Return values
static —filterHeader()
Check the validity of a header.
protected
filterHeader(string $name) : mixed
Parameters
- $name : string
-
The name of the header.
Tags
Return values
mixed —getRequestHeaders()
Function to retrieve HTTP headers.
protected
getRequestHeaders() : array<string|int, mixed>
Return values
array<string|int, mixed> —An array of HTTP headers.
createInputItem()
Reads a value from the $source.
private
createInputItem(array<string|int, mixed> $source, string $name, mixed $default, string|null $type) : InputItem|Inputs
Returns an InputItem or a Collection of InputItem.
Parameters
- $source : array<string|int, mixed>
-
The GET or POST variables.
- $name : string
-
The variable name.
- $default : mixed
-
The default value if the variable is not found.
- $type : string|null
-
The type of input (e.g., GET, POST, SESSION, COOKIE) or null if not from the listed sources.