RequestTrait
Request Trait
Additional methods to make a PSR-7 Request class compliant with the framework's own RequestInterface.
Tags
Table of Contents
Properties
- $config : App
- Configuration settings.
- $globals : array{get?: array, post?: array, request?: array, cookie?: array, server?: array}
- Stores values we've retrieved from PHP globals.
- $ipAddress : string
- IP address of the current user.
Methods
- fetchGlobal() : mixed
- Fetches one or more items from a global, like cookies, get, post, etc.
- getEnv() : mixed
- Fetch an item from the $_ENV array.
- getIPAddress() : string
- Gets the user's IP address.
- getServer() : mixed
- Fetch an item from the $_SERVER array.
- setGlobal() : $this
- Allows manually setting the value of PHP global, like $_GET, $_POST, etc.
- isFromTrustedProxy() : bool
- Checks if the request comes from one of the trusted proxies configured in Config\App::$proxyIPs.
- populateGlobals() : void
- Saves a copy of the current state of one of several PHP globals, so we can retrieve them later.
- checkIPAgainstProxy() : bool
- Checks if the given IP address matches the trusted proxy entry, which may be a single IP address or a subnet in CIDR notation.
- getClientIP() : string|null
- Gets the client IP address from the HTTP header.
Properties
$config
Configuration settings.
protected
App
$config
$globals
Stores values we've retrieved from PHP globals.
Use the Superglobals service instead
protected
array{get?: array, post?: array, request?: array, cookie?: array, server?: array}
$globals
= []
$ipAddress
IP address of the current user.
Will become private in a future release
protected
string
$ipAddress
= ''
Methods
fetchGlobal()
Fetches one or more items from a global, like cookies, get, post, etc.
public
fetchGlobal("cookie"|"get"|"post"|"request"|"server" $name[, array<string|int, mixed>|int|string|null $index = null ][, int|null $filter = null ][, array<string|int, mixed>|int|null $flags = null ]) : mixed
Can optionally filter the input when you retrieve it by passing in a filter.
If $type is an array, it must conform to the input allowed by the filter_input_array method.
http://php.net/manual/en/filter.filters.sanitize.php
Parameters
- $name : "cookie"|"get"|"post"|"request"|"server"
-
Superglobal name (lowercase)
- $index : array<string|int, mixed>|int|string|null = null
- $filter : int|null = null
-
Filter constant
- $flags : array<string|int, mixed>|int|null = null
-
Options
getEnv()
Fetch an item from the $_ENV array.
public
getEnv([array<string|int, mixed>|string|null $index = null ][, int|null $filter = null ][, array<string|int, mixed>|int|null $flags = null ]) : mixed
This method does not work from the beginning. Use env().
Parameters
- $index : array<string|int, mixed>|string|null = null
-
Index for item to be fetched from $_ENV
- $filter : int|null = null
-
A filter name to be applied
- $flags : array<string|int, mixed>|int|null = null
getIPAddress()
Gets the user's IP address.
public
getIPAddress() : string
Return values
string —IP address if it can be detected. If the IP address is not a valid IP address, then will return '0.0.0.0'.
getServer()
Fetch an item from the $_SERVER array.
public
getServer([array<string|int, mixed>|string|null $index = null ][, int|null $filter = null ][, array<string|int, mixed>|int|null $flags = null ]) : mixed
Parameters
- $index : array<string|int, mixed>|string|null = null
-
Index for item to be fetched from $_SERVER
- $filter : int|null = null
-
A filter name to be applied
- $flags : array<string|int, mixed>|int|null = null
setGlobal()
Allows manually setting the value of PHP global, like $_GET, $_POST, etc.
public
setGlobal("cookie"|"get"|"post"|"request"|"server" $name, mixed $value) : $this
Parameters
- $name : "cookie"|"get"|"post"|"request"|"server"
-
Superglobal name (lowercase)
- $value : mixed
Return values
$thisisFromTrustedProxy()
Checks if the request comes from one of the trusted proxies configured in Config\App::$proxyIPs.
protected
isFromTrustedProxy() : bool
Return values
boolpopulateGlobals()
Saves a copy of the current state of one of several PHP globals, so we can retrieve them later.
protected
populateGlobals("cookie"|"get"|"post"|"request"|"server" $name) : void
No longer needs to be called explicitly. Used internally to maintain BC with $globals.
Parameters
- $name : "cookie"|"get"|"post"|"request"|"server"
-
Superglobal name (lowercase)
checkIPAgainstProxy()
Checks if the given IP address matches the trusted proxy entry, which may be a single IP address or a subnet in CIDR notation.
private
checkIPAgainstProxy(string $ip, string $proxyIP) : bool
Supports both IPv4 and IPv6.
Parameters
- $ip : string
- $proxyIP : string
Return values
boolgetClientIP()
Gets the client IP address from the HTTP header.
private
getClientIP(string $header) : string|null
Parameters
- $header : string