Request
in package
Request - HTTP Superglobals Wrapper
Encapsulates $_GET, $_POST, $_FILES, $_SERVER and $_COOKIE to prevent direct superglobal access and facilitate testing.
Features:
- Type-safe access to request data
- Default values support
- File upload handling
- HTTP method detection
- URL/host information
Tags
Table of Contents
Properties
- $cookies : array<string|int, mixed>
- $files : array<string|int, mixed>
- $query : array<string|int, mixed>
- $request : array<string|int, mixed>
- $server : array<string|int, mixed>
Methods
- all() : array<string, mixed>
- Get all parameters (GET + POST merged)
- cookie() : mixed
- Get a cookie value
- create() : self
- Create a custom Request instance (useful for testing)
- createFromGlobals() : self
- Create a Request instance from PHP superglobals
- file() : array<string, mixed>|null
- Get an uploaded file
- get() : mixed
- Get a GET parameter
- getQuery() : array<string, mixed>
- Get all query (GET) parameters
- has() : bool
- Check if a parameter exists (in GET or POST)
- header() : mixed
- Get an HTTP header value
- host() : string
- Get the host name
- isGet() : bool
- Check if this is a GET request
- isPost() : bool
- Check if this is a POST request
- isSecure() : bool
- Check if the request is over HTTPS
- method() : string
- Get the HTTP method
- post() : mixed
- Get a POST parameter
- server() : mixed
- Get a server variable
- uri() : string
- Get the request URI
- __construct() : mixed
Properties
$cookies
private
array<string|int, mixed>
$cookies
$files
private
array<string|int, mixed>
$files
$query
private
array<string|int, mixed>
$query
$request
private
array<string|int, mixed>
$request
$server
private
array<string|int, mixed>
$server
Methods
all()
Get all parameters (GET + POST merged)
public
all() : array<string, mixed>
Return values
array<string, mixed> —All request parameters
cookie()
Get a cookie value
public
cookie(string $key[, mixed $default = null ]) : mixed
Parameters
- $key : string
-
Cookie name
- $default : mixed = null
-
Default value if not set
Return values
mixed —Cookie value or default
create()
Create a custom Request instance (useful for testing)
public
static create([array<string, mixed> $query = [] ][, array<string, mixed> $request = [] ][, array<string, mixed> $files = [] ][, array<string, mixed> $server = [] ][, array<string, mixed> $cookies = [] ]) : self
Parameters
- $query : array<string, mixed> = []
-
GET parameters
- $request : array<string, mixed> = []
-
POST parameters
- $files : array<string, mixed> = []
-
Uploaded files
- $server : array<string, mixed> = []
-
Server variables
- $cookies : array<string, mixed> = []
-
Cookies
Return values
self —New Request with provided values
createFromGlobals()
Create a Request instance from PHP superglobals
public
static createFromGlobals() : self
Return values
self —New Request with current superglobal values
file()
Get an uploaded file
public
file(string $key) : array<string, mixed>|null
Parameters
- $key : string
-
File input name
Return values
array<string, mixed>|null —File data or null if not found
get()
Get a GET parameter
public
get(string $key[, mixed $default = null ]) : mixed
Parameters
- $key : string
-
Parameter name
- $default : mixed = null
-
Default value if not set
Return values
mixed —Parameter value or default
getQuery()
Get all query (GET) parameters
public
getQuery() : array<string, mixed>
Return values
array<string, mixed> —All GET parameters
has()
Check if a parameter exists (in GET or POST)
public
has(string $key) : bool
Parameters
- $key : string
-
Parameter name
Return values
bool —True if parameter exists
header()
Get an HTTP header value
public
header(string $key[, mixed $default = null ]) : mixed
Headers are retrieved from $SERVER with HTTP prefix. Example: 'X-CSRF-Token' becomes 'HTTP_X_CSRF_TOKEN' in $_SERVER
Parameters
- $key : string
-
Header name (e.g., 'X-CSRF-Token')
- $default : mixed = null
-
Default value if not set
Return values
mixed —Header value or default
host()
Get the host name
public
host() : string
Return values
string —Host name (e.g., localhost, example.com)
isGet()
Check if this is a GET request
public
isGet() : bool
Return values
bool —True if GET
isPost()
Check if this is a POST request
public
isPost() : bool
Return values
bool —True if POST
isSecure()
Check if the request is over HTTPS
public
isSecure() : bool
Return values
bool —True if HTTPS
method()
Get the HTTP method
public
method() : string
Return values
string —HTTP method in uppercase (GET, POST, etc.)
post()
Get a POST parameter
public
post(string $key[, mixed $default = null ]) : mixed
Parameters
- $key : string
-
Parameter name
- $default : mixed = null
-
Default value if not set
Return values
mixed —Parameter value or default
server()
Get a server variable
public
server(string $key[, mixed $default = null ]) : mixed
Parameters
- $key : string
-
Server variable name
- $default : mixed = null
-
Default value if not set
Return values
mixed —Server variable value or default
uri()
Get the request URI
public
uri() : string
Return values
string —Request URI (e.g., /index.php?page=home)
__construct()
private
__construct(array<string, mixed> $query, array<string, mixed> $request, array<string, mixed> $files, array<string, mixed> $server, array<string, mixed> $cookies) : mixed
Parameters
- $query : array<string, mixed>
-
GET parameters
- $request : array<string, mixed>
-
POST parameters
- $files : array<string, mixed>
-
Uploaded files
- $server : array<string, mixed>
-
Server variables
- $cookies : array<string, mixed>
-
Cookies