PHP Luminova: Incoming HTTP Request Object
Incoming HTTP request class, offering a secure and consistent interface to retrieve query parameters, form inputs, headers, files, and server information, enabling easy request handling.
The Request class represents an incoming HTTP request, encapsulating all relevant data sent by the client, including headers, query parameters, form inputs, cookies, server variables, and uploaded files. It provides a structured and secure interface for accessing and manipulating request data, ensuring consistency across web applications.
In addition to basic data retrieval, the Request class includes built-in authentication mechanisms, allowing validation of requests based on trusted origins, proxy headers, or allowed domain rules. This makes it easier to implement security measures such as CSRF protection, origin checks, and proxy validation without adding additional layers of complexity.
By abstracting low-level server interactions, the Request class helps request handling, improves application security, and simplifies application development in HTTP-based services.
Accessing Object
The request object can be accessed in multiple ways, depending on the context. You can use the global helper function, the Factory class, or the Controller class.
Access Methods
1. Using Global Helper Function
use function Luminova\Funcs\request;
$request = request();2. Accessing Through the Factory Class
use Luminova\Application\Factory;
$request = Factory::request();3. Initializing the Request Instance Directly
use Luminova\Http\Request;
$request = new Request();4. Accessing in Controller Classes
If your controller extends Luminova\Base\Controller.
$this->request->fooMethod();Class Definition
- Class namespace:
Luminova\Http\Request - This class implements: \Luminova\Interface\RequestInterface, \Luminova\Interface\LazyObjectInterface, \Luminova\Interface\Arrayable, \Stringable
Properties
server
Http server params instance.
public readonly ?Luminova\Http\Server $server;header
Http request header instance.
public readonly ?Luminova\Http\Header $header;agent
Browser request user-agent information.
public readonly ?Luminova\Http\UserAgent $agent;Methods
constructor
Create a new HTTP request representing an incoming client request.
This constructor optionally accept request properties like method, URI, body, files, server parameters, and headers. allowing you to create a custom HTTP request. Files are normalized to PSR-7 UploadedFileInterface instances.
public __construct(
?string $method = null,
Psr\Http\Message\UriInterface|string|null $uri = null,
Psr\Http\Message\StreamInterface|array $body = [],
Psr\Http\Message\UploadedFileInterface[]|array<string,array> $files = [],
?array<string,mixed> $cookies = null,
?array<string,mixed> $serverParams = null,
?array<string,mixed> $headers = null,
?string $protocolVersion = null
)Parameters:
| Parameter | Type | Description |
|---|---|---|
$method | string|null | The HTTP method (e.g., GET, Luminova\Http\Method::POST). |
$uri | UriInterface|string|null | The request URI object or a string containing path and query. |
$body | StreamInterface|array<string,mixed> | Request body. Can be a PSR-7 stream or an associative array (form data, JSON, etc.). |
$files | UploadedFileInterface[]\array<string,array> | Uploaded files object or an associative array (e.g., ['image' => [...]]).. |
$cookies | array<string,mixed> | Optional associative array of Cookies (e.g, $_COOKIE). |
$serverParams | array<string,mixed>|null | Optional server parameters (e.g., $_SERVER). |
$headers | array<string,mixed>|null | Optional HTTP headers. If not provided, they will be extracted from apache_request_headers() or $_SERVER. |
$protocolVersion | string|null | HTTP protocol version (default: null). |
Example
Create a new request-response object by passing appropriate values.
$request = new Luminova\Http\Request(
method: 'POST',
uri: '/api/v1/users',
body: ['name' => 'John Doe', 'email' => '[email protected]'],
files: [
'image' => [
'name' => 'profile.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/tmp/phpYzdqkD',
'error' => 0,
'size' => 123456
]
],
serverParams: [
'REQUEST_METHOD' => 'POST',
'REMOTE_ADDR' => '192.168.1.10',
'HTTP_USER_AGENT' => 'Mozilla/5.0',
'SERVER_PROTOCOL' => 'HTTP/1.1',
'SERVER_NAME' => 'example.com',
'REQUEST_URI' => '/api/v1/users',
],
headers: [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer some-token',
'Accept' => 'application/json',
'X-Custom-Header' => 'CustomValue'
]
);
echo $request->getPost('name') //John Doe
// Or using ANY
echo $request->input('name') //John DoeSupported Array File Structures
PHP File Upload Structure:
[
'images' => [
'name' => ['file1.jpg', 'file2.png', 'file3.gif'],
'type' => ['image/jpeg', 'image/png', 'image/gif'],
'tmp_name' => ['/tmp/phpYzdqkD', '/tmp/phpeEwEWq', '/tmp/php7sdfXy'],
'error' => [0, 0, 0],
'size' => [123456, 234567, 345678]
]
];Flat File Structure:
[
'images' => [
[
'name' => 'file1.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/tmp/phpYzdqkD',
'error' => 0,
'size' => 123456
],
[
'name' => 'file2.png',
'type' => 'image/png',
'tmp_name' => '/tmp/phpeEwEWq',
'error' => 0,
'size' => 234567
],
[
'name' => 'file3.gif',
'type' => 'image/gif',
'tmp_name' => '/tmp/php7sdfXy',
'error' => 0,
'size' => 345678
]
]
];Note: This structure may be developer-friendly, but it isn't the standard way PHP handles uploaded files.
getInstance
Returns a shared instance of HTTP request representing an incoming client request.
public static getInstance(
?string $method = null,
Psr\Http\Message\UriInterface|string|null $uri = null,
Psr\Http\Message\StreamInterface|array $body = [],
Psr\Http\Message\UploadedFileInterface[]|array<string,array> $files = [],
?array<string,mixed> $cookies = null,
?array<string,mixed> $serverParams = null,
?array<string,mixed> $headers = null,
?string $protocolVersion = null
): \Luminova\Interface\RequestInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$method | string|null | The HTTP method (e.g., GET, Luminova\Http\Method::POST). |
$uri | UriInterface|string|null | The request URI object or a string containing path and query. |
$body | StreamInterface|array<string,mixed> | Request body. Can be a PSR-7 stream or an associative array (form data, JSON, etc.). |
$files | UploadedFileInterface[]\array<string,array> | Uploaded files object or an associative array (e.g., ['image' => [...]]).. |
$cookies | array<string,mixed> | Optional associative array of Cookies (e.g, $_COOKIE). |
$serverParams | array<string,mixed>|null | Optional server parameters (e.g., $_SERVER). |
$headers | array<string,mixed>|null | Optional HTTP headers. If not provided, they will be extracted from apache_request_headers() or $_SERVER. |
$protocolVersion | string|null | HTTP protocol version (default: null). |
Return Value:
RequestInterface - Return new or shared instance of request object.
toString
Converts the request body to a raw string format based on the content type.
public toString(): stringReturn Value:
string - Return the raw string representation of the request body.
Supported Content Types:
application/x-www-form-urlencoded: Converts the body to a URL-encoded query string.application/json: Converts the body to a JSON string.multipart/form-data: Converts the body to a multipart/form-data format.
toMultipart
Converts the request body to a multipart/form-data string format.
public toMultipart(): stringReturn Value:
string - Return the multipart form-data representation of the request body.
setField
Set a specific field in the request body for the given HTTP method.
public setField(string $field, mixed $value, ?string $method = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$field | string|null | The name of the field to set. |
$value | mixed | The value to assign to the field. |
$method | string|null | Optional HTTP method, if null the current request method will be used (e.g, GET, POST). |
Return Value:
self - Returns the instance request class.
removeField
Remove a specific field from the request body for the given HTTP method.
public removeField(string $field, ?string $method = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$field | string | The name of the field to remove. |
$method | string|null | Optional HTTP method, if null the current request method will be used (e.g, GET, POST). |
Return Value:
self - Returns the instance request class.
input
Get a field value from Any valid incoming HTTP request method.
If $method is provided, it checks field in the specified HTTP method, otherwise it use the current request method.
public input(string $field, mixed $default = null, ?string $method = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$field | string | The input field name to retrieve the value value from. |
$default | mixed | An optional default value to return if the field is not found (default: null). |
$method | string|null | Optional HTTP method to retrieve valid from (default: null as ANY). |
Return Value:
mixed - Return the value from HTTP request method body based on field name or default value.
Example:
$username = $request->input('username');getGet
Get a field value from HTTP GET request method or entire fields if $field parameter is null.
public getGet(string $field, mixed $default = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$field | string | The field key to retrieve the value from. |
$default | mixed | An optional default value to return if the field is not found (default: null). |
Return Value:
mixed - Return the value from HTTP request method body based on field name.
getPost
Get a field value from HTTP POST request method or entire fields if $field parameter is null.
public getPost(string $field, mixed $default = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$field | string | The field key to retrieve the value from. |
$default | mixed | An optional default value to return if the field is not found (default: null). |
Return Value:
mixed - Return the value from HTTP request method body based on field name.
getPut
Get a value from the PUT request or entire fields if $field parameter is null.
public getPut(string $field, mixed $default = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$field | string | The field key to retrieve the value from. |
$default | mixed | An optional default value to return if the field is not found (default: null). |
Return Value:
mixed - Return the value from HTTP request method body based on field name.
getOptions
Get a value from the OPTIONS request or entire fields if $field parameter is null.
public getOptions(string $field, mixed $default = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$field | string | The field key to retrieve the value from. |
$default | mixed | An optional default value to return if the field is not found (default: null). |
Return Value:
mixed - Return the value from HTTP request method body based on field name.
getPatch
Get a value from the PATCH request method or entire fields if $field parameter is null.
public getPatch(string $field, mixed $default = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$field | string | The field key to retrieve the value from. |
$default | mixed | An optional default value to return if the field is not found (default: null). |
Return Value:
mixed - Return the value from HTTP request method body based on field name.
getHead
Get a value from the HEAD request method or entire fields if $field parameter is null.
public getHead(string $field, mixed $default = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$field | string | The field key to retrieve the value from. |
$default | mixed | An optional default value to return if the field is not found (default: null). |
Return Value:
mixed - Return the value from HTTP request method body based on field name.
getConnect
Get a value from the CONNECT request method or entire fields if $field parameter is null.
public getConnect(string $field, mixed $default = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$field | string | The field key to retrieve the value from. |
$default | mixed | An optional default value to return if the field is not found (default: null). |
Return Value:
mixed - Return the value from HTTP request method body based on field name.
getTrace
Get a value from the TRACE request method or entire fields if $field parameter is null.
public getTrace(string $field, mixed $default = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$field | string | The field key to retrieve the value from. |
$default | mixed | An optional default value to return if the field is not found (default: null). |
Return Value:
mixed - Return the value from HTTP request method body based on field name.
getPropfind
Get a value from the PROPFIND request method or entire fields if $field parameter is null.
public getPropfind(string $field, mixed $default = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$field | string | The field key to retrieve the value from. |
$default | mixed | An optional default value to return if the field is not found (default: null). |
Return Value:
mixed - Return the value from HTTP request method body based on field name.
getMkcol
Get a value from the MKCOL request method or entire fields if $field parameter is null.
public getMkcol(string $field, mixed $default = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$field | string | The field key to retrieve the value from. |
$default | mixed | An optional default value to return if the field is not found (default: null). |
Return Value:
mixed - Return the value from HTTP request method body based on field name.
getCopy
Get a value from the COPY request method or entire fields if $field parameter is null.
public getCopy(string $field, mixed $default = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$field | string | The field key to retrieve the value from. |
$default | mixed | An optional default value to return if the field is not found (default: null). |
Return Value:
mixed - Return the value from HTTP request method body based on field name.
getMove
Get a value from the MOVE request method or entire fields if $field parameter is null.
public getMove(string $field, mixed $default = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$field | string | The field key to retrieve the value from. |
$default | mixed | An optional default value to return if the field is not found (default: null). |
Return Value:
mixed - Return the value from HTTP request method body based on field name.
getLock
Get a value from the LOCK request method or entire fields if $field parameter is nu