Luminova Framework

HTTP Request

Last updated: 2024-05-08 06:22:51

The Request class represents an incoming HTTP request received by a server. It encapsulates various aspects of the request, such as headers, query parameters, form data, uploaded files, and more. This class provides a convenient and structured way to access and manipulate incoming HTTP requests, with built-in authentication mechanisms to validate requests based on factors such as origin, proxies, or allowed domains.


Properties

server

Http server instance.

public null|\Luminova\Http\Server $server

header

Http request header instance.

public null|\Luminova\Http\Header $header

agent

Browser request user-agent information.

public null|\Luminova\Http\UserAgent $agent

Methods

getGet

Get a value from the GET request.

public getGet(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe key of the value to retrieve.
$defaultmixed(optional) The default value to return if the key is not found.

Return Value:

mixed - Response from HTTP GET request.


getPost

Get a value from the POST request.

public getPost(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe key of the value to retrieve.
$defaultmixed(optional) The default value to return if the key is not found.

Return Value:

mixed - Response from HTTP POST request.


getPut

Get a value from the PUT request.

public getPut(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe key of the value to retrieve.
$defaultmixed(optional) The default value to return if the key is not found.

Return Value:

mixed - Response from HTTP PUT request.


getOptions

Get a value from the OPTIONS request.

public getOptions(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe key of the value to retrieve.
$defaultmixed(optional) The default value to return if the key is not found.

Return Value:

mixed - Response from HTTP OPTIONS request.


getPatch

Get a value from the PATCH request.

public getPatch(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe key of the value to retrieve.
$defaultmixed(optional) The default value to return if the key is not found.

Return Value:

mixed - Response from HTTP PATCH request.


getHead

Get a value from the HEAD request.

public getHead(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe key of the value to retrieve.
$defaultmixed(optional) The default value to return if the key is not found.

Return Value:

mixed - Response from HTTP HEAD request.


getConnect

Get a value from the CONNECT request.

public getConnect(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe key of the value to retrieve.
$defaultmixed(optional) The default value to return if the key is not found.

Return Value:

mixed - Response from HTTP CONNECT request.


getTrace

Get a value from the TRACE request.

public getTrace(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe key of the value to retrieve.
$defaultmixed(optional) The default value to return if the key is not found.

Return Value:

mixed - Response from HTTP TRACE request.


getPropfind

Get a value from the PROPFIND request.

public getPropfind(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe key of the value to retrieve.
$defaultmixed(optional) The default value to return if the key is not found.

Return Value:

mixed - Response from HTTP PROPFIND request.


getMkcol

Get a value from the MKCOL request.

public getMkcol(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe key of the value to retrieve.
$defaultmixed(optional) The default value to return if the key is not found.

Return Value:

mixed - Response from HTTP MKCOL request.


getCopy

Get a value from the COPY request.

public getCopy(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe key of the value to retrieve.
$defaultmixed(optional) The default value to return if the key is not found.

Return Value:

mixed - Response from HTTP COPY request.


getMove

Get a value from the MOVE request.

public getMove(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe key of the value to retrieve.
$defaultmixed(optional) The default value to return if the key is not found.

Return Value:

mixed - Response from HTTP MOVE request.


getLock

Get a value from the LOCK request.

public getLock(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe key of the value to retrieve.
$defaultmixed(optional) The default value to return if the key is not found.

Return Value:

mixed - Response from HTTP LOCK request.


getUnlock

Get a value from the UNLOCK request.

public getUnlock(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe key of the value to retrieve.
$defaultmixed(optional) The default value to return if the key is not found.

Return Value:

mixed - Response from HTTP UNLOCK request.


getArray

Get a value from the request method context array.

public getArray(string $method, string $key, array $default = []): array

Parameters:

ParameterTypeDescription
$methodstringHTTP request method context.
$keystringRequest body key.
$defaultarrayDefault value.

Return Value:

array - Return array of HTTP request method key values.

Throws:


getBody

Get the request body as an array or json object.

public getBody(bool $object = false): array|object

Parameters:

ParameterTypeDescription
$objectboolWhether to return an array or a json object (default: false).

Return Value:

array|object - Return the request body as an array or json object.


getFile

Get the uploaded file information by its input name.

public getFile(string $name): \Luminova\Http\File|false

Parameters:

ParameterTypeDescription
$namestringFile input name.

Return Value:

\Luminova\Http\File|false - Uploaded file information or false if file not found.

See Also:

File Upload Object


getFiles

Get the uploaded files information irritable array.

public getFiles(): false|array<int,\Luminova\Http\File>

Return Value:

false|array<int,\Luminova\Http\File> - Uploaded files information or false if no files found.

See Also:

File Upload Object


getMethod

Get the request method.

public getMethod(): string

Return Value:

string - Return the request method in lowercased.


isGet

Check if the request method is GET.

public isGet(): bool

Return Value:

bool - Returns true if the request method is GET, false otherwise.


isPost

Check if the request method is POST.

public isPost(): bool

Return Value:

bool - Returns true if the request method is POST, false otherwise.


isMethod

Check if the request method is the provided method.

public isMethod(string $method): bool

Parameters:

ParameterTypeDescription
$methodstringThe method to check against.

Return Value:

bool - Returns true if the request method matches the provided method, false otherwise.


getContentType

Get the request content type.

public getContentType(): string

Return Value:

string - The request content type.


getAuth

Get HTTP request header authorization [HTTP_AUTHORIZATION, Authorization].

public getAuth(): string|null

Return Value:

string|null - Return the authorization header value or null if no authorization header was sent.


isCommand

Check to see if a request was made from the command line.

public isCommand(): bool

Return Value:

bool - Return true if the request was made from the command line.


isSecure

Check if the current connection is secure

public isSecure(): bool

Return Value:

bool - Return true if the connection is secure false otherwise.


isAJAX

Check if request is ajax request, see if a request contains the HTTP_X_REQUESTED_WITH header.

public isAJAX(): bool

Return Value:

bool - Return true if request is ajax request, false otherwise


isApi

Check if the request URL indicates an API endpoint.

public isApi(string|null $url = null): bool

This method checks if the URL path starts with '/api' or 'public/api'.

Parameters:

ParameterTypeDescription
$urlstring|nullThe request URL to check.

Return Value:

bool - Returns true if the URL indicates an API endpoint, false otherwise.


getQuery

Get the request url query string.

public getQuery(): string

Return Value:

string - Return url query string.


getQueries

Get current url query parameters as an array.

public getQueries(): array<string,mixed>

Return Value:

array<string,mixed> - Url query parameters.


getUri

Get current request url

public getUri(): string

Return Value:

string - Return request url or null if not set.


getPaths

Get current request url path information

public getPaths(): string

Return Value:

string - Return request url paths.


getRequestUri

Returns the requested URI (path and query string).

public getRequestUri(): string

Return Value:

string - The raw URI (i.e. not URI decoded)


getHost

Get current hostname without port, if allowed host is set it will check if host is in allowed list or patterns.

public getHost(bool $extension = false): string

Parameters:

ParameterTypeDescription
$extensionboolShould throw an exception if invalid host or not allowed host (default: false).

Return Value:

string - Return hostname.

Throws:


getHostname

Get current hostname with port if port is available.If allowed host is set it will check if host is in allowed list or patterns.

public getHostname(bool $extension = false, bool $port = true): string

Parameters:

ParameterTypeDescription
$extensionboolShould throw an exception if invalid host or not allowed host (default: false).
$portboolShould return hostname with port (default: true).

Return Value:

string - Return hostname.

Throws:


getOrigin

Get the origin domain if list of trusted origin domains are specified.

public getOrigin(): string|null

It will check if the origin is a trusted origin domain.

Return Value:

string|null - Origin domain if found and trusted, otherwise null.


getPort

Get the request origin port.

public getPort(): int|string|null

Return Value:

int|string|null - Can be a string if fetched from the server bag

Check if X-Forwarded-Port header exists and use if available.

If not available check for server-port header if also not available return NULL as default.


getScheme

Gets the request's scheme.

public getScheme(): string

getProtocol

Gets the request server protocol (e.g: HTTP/1.1).

public getProtocol(): string

getBrowser

Get user browser information.

public getBrowser(): string

Return Value:

string - Return browser name and platform.


getUserAgent

Get browser user-agent information.

public getUserAgent(?string $useragent = null): UserAgent

Parameters:

ParameterTypeDescription
$useragentstring|nullThe User Agent string. If not provided, it defaults to $_SERVER['HTTP_USER_AGENT'].

Return Value:

UserAgent - Return user agent instance.


isSameOrigin

Check if the request's origin matches the current host.

public isSameOrigin(bool $subdomains = true): bool

Parameters:

ParameterTypeDescription
$subdomainsboolWhether to consider subdomains or not. Default is true.

Return Value:

bool - Returns true if the request's origin matches the current host as define in env app.hostname, false otherwise.


isTrusted

Check if the given (hostnames, origins, proxy ip or subnet) matches any of the trusted patterns.

public static isTrusted(string $input, string $context = 'hostname'): bool

Parameters:

ParameterTypeDescription
$inputstringThe domain, origin or ip address to check.
$contextstringThe context to check (hostname, origin or proxy).

Return Value:

bool - Return true if the input is trusted, false otherwise.

Throws:


isTrustedProxy

Check whether this request origin ip address is from a trusted proxy.

public isTrustedProxy(): bool

Return Value:

bool - Return true if the request origin ip address is trusted false otherwise.


isTrustedOrigin

Check whether this request origin is from a trusted origins.

public isTrustedOrigin(): bool

Return Value:

bool - Return true if the request origin is trusted false otherwise.