Request
Request
Application-level HTTP request container and helper API.
class Request implements \Hazaar\Application\RequestInterfaceThis class captures server state ($_SERVER), query/form parameters ($_GET, $_POST), parsed headers, and request body content for the current execution context. It also exposes convenience methods for common request inspection and data-access tasks used throughout the framework controller lifecycle.
Key responsibilities:
- Generate a per-request identifier for tracing/log correlation.
- Normalize request path and detect the HTTP method.
- Provide typed accessors for parameters and headers.
- Parse request payloads for JSON, form-url-encoded, and XML content types.
- Store request-scoped custom attributes.
If you need a standalone HTTP client-style request object, use {@see \Hazaar\HTTP\Request}.
Properties
id
Unique identifier for this request instance.
public string $idGenerated at construction time and typically used for logging correlation.
body
public string $bodysession
Indicates whether session handling is enabled for this request.
public bool $sessiondispatched
Tracks whether the request has been dispatched to a controller/action.
protected bool $dispatchedpublic $method
Request method.
protected string $method = 'GET'server
Array of server variables, one line per element. Normally comes from $_SERVER but can be set via the constructor.
private array $serverget
Array of GET parameters, one line per element. Normally comes from $_GET but can be set via the constructor.
private array $getpost
Array of POST parameters, one line per element. Normally comes from $_POST but can be set via the constructor.
private array $postpath
The requested path.
private string $pathheaders
Array of headers, one line per element.
private array $headersattributes
An array of custom attributes set on the request.
private array $attributesMethods
__construct
The Application HTTP request object.
public __construct(?array $server, ?array $get, ?array $post): voidThis constructor will initialise the request object. It will parse the request URI and any request parameters and set them as properties of the object.
The constructor will also get all the request headers and the request content and from there will use the Hazaar\Application\Request parent class to determine the name of the Controller and Action that is being requested via it's evaluate() method.
Parameters
| Parameter | Type | Description |
|---|---|---|
$server | array | Optional reference to $_SERVER |
$get | array | Optional reference to $_GET |
$post | array | Optional reference to $_POST |
__get
Magic method to get the value of a property.
public __get(string $key): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the name of the property to get |
getId
Retrieves the unique identifier for the request.
public getId(): stringisGet
public isGet(): boolTest if the request method is GET. This is a convenience method for quickly determining the request method.
isPut
public isPut(): boolTest if the request method is PUT. This is a convenience method for quickly determining the request method.
isPost
public isPost(): boolTest if the request method is POST. This is a convenience method for quickly determining the request method.
isDelete
public isDelete(): boolTest if the request method is DELETE. This is a convenience method for quickly determining the request method.
getHeaders
public getHeaders(): voidGet all the HTTP request headers sent by the client browser.
hasHeader
public hasHeader(string $header): boolCheck if a header was sent in the HTTP request.
Parameters
| Parameter | Type | Description |
|---|---|---|
$header | string |
getHeader
public getHeader(string $header, bool $stripOptions): ?stringGet a single header value
Parameters
| Parameter | Type | Description |
|---|---|---|
$header | string | |
$stripOptions | bool |
setHeader
Sets a header value for the request.
public setHeader(string $header, string $value): voidParameters
| Parameter | Type | Description |
|---|---|---|
$header | string | the name of the header to set |
$value | string | the value to assign to the header |
getContentType
Retrieves the value of the 'Content-Type' header from the request.
public getContentType(): ?stringisXmlHttpRequest
public isXmlHttpRequest(): boolTest if the request originated from an XMLHttpRequest object. This object is used when sending an AJAX request from withing a JavaScript function. All of the major JavaScript libraries (jQuery, extJS, etc) will set the X-Requested-With header to indicate that the request is an AJAX request.
Using this in your application will allow you to determine how to respond to the request. For example, you might want to forgo rendering a view and instead return a JSON response.
redirectURI
public redirectURI(): ?stringReturns the URI of the page this request was redirected from.
getRequestBody
public getRequestBody(): stringReturns the body of the request. This will normally be null unless the request is a POST or PUT.
getJSONBody
public getJSONBody(?bool $assoc, int $depth = 512): mixedReturns the JSON decoded body of the request. This will normally be null unless the request is a POST or PUT and content-type is application/json.
Parameters
| Parameter | Type | Description |
|---|---|---|
$assoc | bool | |
$depth | int |
getURLBody
Parse a URL-encoded request body into key/value parameters.
public getURLBody(): mixedSupported content types include text/html and application/x-www-form-urlencoded.
getXMLBody
Parse the request body as XML.
public getXMLBody(): ?ElementgetRemoteAddr
Get the remote IP address of the requesting host.
public getRemoteAddr(): ?stringThis will try to determine the correct IP to return. By default it will return the $_SERVER['REMOTE_ADDR'] value, but if the connection is via a reverse proxy (such as Haproxy) then it will possibly have the standard X-Forwarded-For header, so if that header exists then that value will be returned.
isMobileDevice
Detect if a request originated on a mobile device.
public isMobileDevice(): boolThis method will return true to indicate that the requesting device is a mobile browser. It uses the freely available script from detectmobilebrowsers.com
referer
Get the referer URL from the HTTP request headers.
public referer(): ?stringgetPath
Return the request path.
public getPath(bool $stripFilename): stringParameters
| Parameter | Type | Description |
|---|---|---|
$stripFilename | bool | If true, this will cause the function to return anything before the last '/' (including the '/') which is the full directory path name. (Similar to dirname()). |
getProtocol
Return the HTTP protocol string used by the server for this request.
public getProtocol(): stringgetReferer
Return the request referer from server variables.
public getReferer(): ?stringgetUserAgent
Return the request user-agent string.
public getUserAgent(): ?stringsetPath
Sets the path of the request.
public setPath(string $path): voidParameters
| Parameter | Type | Description |
|---|---|---|
$path | string | the path of the request |
get
Retrieve a value from the GET parameters.
public get(string $key): ?stringThis method attempts to retrieve the value of the specified key from the $_GET superglobal. If the key does not exist, it returns null.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key of the GET parameter to retrieve |
post
Retrieve a value from the POST parameters.
public post(string $key): ?stringThis method attempts to retrieve the value of the specified key from the $_POST superglobal. If the key does not exist, it returns null.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key of the POST parameter to retrieve |
param
Retrieve a parameter value from the request (GET or POST).
public param(string $key): ?stringThis method attempts to retrieve the value of the specified key from the GET parameters first. If the key does not exist in GET, it will then attempt to retrieve it from the POST parameters.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key of the parameter to retrieve |
getInt
Retrieve an integer value from the request.
public getInt(string $key): ?intThis method attempts to retrieve a parameter value from the request (via GET or POST), and returns it as an integer. If the parameter is not set, null is returned.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The key of the request value to return |
getFloat
Retrieve an float value from the request.
public getFloat(string $key): floatThe most common requests will not provide data typing and data value will always be a string. This method will automatically return the requested value as an float unless it is NULL or not set. In which case either NULL or the default value will be returned.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key of the request value to return |
getBool
Retrieve a boolean value from the request.
public getBool(string $key): boolThis method attempts to retrieve a parameter value from the request (via GET or POST), and returns it as a boolean using Hazaar\Util\Boolean::from().
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The key of the request value to return |
has
Check to see if a request value has been set.
public has(string $keys, bool $checkAny): boolParameters
| Parameter | Type | Description |
|---|---|---|
$keys | string | the key of the request value to check for |
$checkAny | bool | The check type when $key is an array. TRUE means that ANY key must exist. FALSE means ALL keys must exist. |
getParams
Return an array of request parameters as key/value pairs.
public getParams(?array $filterIn, ?array $filterOut): voidParameters
| Parameter | Type | Description |
|---|---|---|
$filterIn | array | only include parameters with keys specified in this filter |
$filterOut | array | exclude parameters with keys specified in this filter |
hasParams
Check if the request has any parameters.
public hasParams(): boolcount
Returns the number of parameters in the request.
public count(): intisEmpty
Checks if the request has no parameters.
public isEmpty(): boolgetMethod
public getMethod(): stringReturns the method used to initiate this request on the server. .
getRequestHeaders
Get the current request headers.
public getRequestHeaders(): voidThis function will return the current request headers as an associative array.
setAttribute
Sets a request attribute.
public setAttribute(string $key, mixed $value): voidThis method allows storing custom data or attributes within the request object. Attributes are often used to pass data between different parts of the application during the lifecycle of a request.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the name of the attribute to set |
$value | mixed | the value of the attribute |
getAttribute
Retrieves a specific attribute by key.
public getAttribute(string $key): mixedThis method searches the request attributes for the given key and returns its value. If the key does not exist in the attributes list, it returns null.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key of the attribute to retrieve |
Generated by Hazaar API Doc Generator on Tue, 21 Apr 2026 04:00:25 +0000