Request
Request
Controller HTTP Request Class
The HTTP controller request class is a representational object for an HTTP request. The Application object will create a HTTP request object upon each execution. This object contains all details of the current request including request data, headers and any request body content.
If you want to generate your own HTTP request object to pass to another method or function that requires one, see [[Hazaar\Http\Request]].
Properties
pathParam
public string $pathParam = 'hzpath'
queryParam
public string $queryParam = 'hzqs'
body
public string $body
dispatched
protected bool $dispatched
params
protected array $params
public $
method
Request method.
protected string $method = 'GET'
path
The requested path.
private string $path
headers
Array of headers, one line per element.
private array $headers
Methods
__construct
public __construct(?array $server, ?array $request, bool $processRequestBody = false): void
The HTTP request object constructor.
Parameters
Parameter | Type | Description |
---|---|---|
$server | array | Optional reference to $_SERVER |
$request | array | Optional reference to $_REQUEST |
$processRequestBody | bool |
__get
Magic method to get the value of a property.
public __get(string $key): mixed
Parameters
Parameter | Type | Description |
---|---|---|
$key | string | the name of the property to get |
__unset
Unsets a value from the request object.
public __unset(string $key): void
This method removes a value from the request object using the specified key.
Parameters
Parameter | Type | Description |
---|---|---|
$key | string | the key of the value to unset |
init
public init(?array $server, ?array $request, bool $processRequestBody = false): string
The HTTP init method takes only a single optional argument which is the request array provided by PHP ($_REQUEST).
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 |
$request | array | Optional reference to $_REQUEST |
$processRequestBody | bool |
isGet
public isGet(): bool
Test if the request method is GET. This is a convenience method for quickly determining the request method.
isPut
public isPut(): bool
Test if the request method is PUT. This is a convenience method for quickly determining the request method.
isPost
public isPost(): bool
Test if the request method is POST. This is a convenience method for quickly determining the request method.
isDelete
public isDelete(): bool
Test if the request method is DELETE. This is a convenience method for quickly determining the request method.
getHeaders
public getHeaders(): void
Get all the HTTP request headers sent by the client browser.
hasHeader
public hasHeader(string $header): bool
Check if a header was sent in the HTTP request.
Parameters
Parameter | Type | Description |
---|---|---|
$header | string |
getHeader
public getHeader(string $header): string
Get a single header value
Parameters
Parameter | Type | Description |
---|---|---|
$header | string |
getContentType
Return the current request content type.
public getContentType(): string
This is a helpful method for doing a few things in one go as it will only return a content type if the request is a POST method. Otherwise it will safely return a FALSE value.
isXmlHttpRequest
public isXmlHttpRequest(): bool
Test 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(): ?string
Returns the URI of the page this request was redirected from.
getRequestBody
public getRequestBody(): string
Returns 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): mixed
Returns 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 |
getRemoteAddr
public getRemoteAddr(): ?string
isMobileDevice
Detect if a request originated on a mobile device.
public isMobileDevice(): bool
This 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(): ?string
getPath
Return the request path.
public getPath(bool $stripFilename = false): string
Parameters
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()). |
setPath
Sets the path of the request.
public setPath(string $path): void
Parameters
Parameter | Type | Description |
---|---|---|
$path | string | the path of the request |
get
Retrieve a request value.
public get(string $key, mixed $default): mixed
These values can be sent in a number of ways.
- In a query string. eg: http://youhost.com/controller?key=value
- As form POST data.
- As JSON encoded request body.
Only JSON encoded request bodies support data typing. All other request values will be strings.
Parameters
Parameter | Type | Description |
---|---|---|
$key | string | The data key to retrieve |
$default | mixed | if the value is not set, use this default value |
getInt
Retrieve an integer value from the request.
public getInt(string $key, ?int $default): ?int
The 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 integer 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 |
$default | int | a default value to use if the value is NULL or not set |
getFloat
Retrieve an float value from the request.
public getFloat(string $key, ?float $default): float
The 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 |
$default | float | a default value to use if the value is NULL or not set |
getBool
Retrieve an boolean value from the request.
public getBool(string $key, NULL $default): bool
The 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 boolean unless it is NULL or not set. In which case either NULL or the default value will be returned.
This internally uses the boolify() function so the usual bool strings are supported (t, f, true, false, 0, 1, on, off, etc).
Parameters
Parameter | Type | Description |
---|---|---|
$key | string | the key of the request value to return |
$default | NULL | a default value to use if the value is NULL or not set |
has
Check to see if a request value has been set.
public has(string $keys, bool $check_any = false): bool
Parameters
Parameter | Type | Description |
---|---|---|
$keys | string | the key of the request value to check for |
$check_any | bool | The check type when $key is an array. TRUE means that ANY key must exist. FALSE means ALL keys must exist. |
set
Set a request value.
public set(string $key, mixed $value): void
This would not normally be used and has no internal implications on how the application will function as this data is not processed in any way. However setting request data may be useful in your application when reusing/repurposing controller actions so that they may be called from somewhere else in your application.
Parameters
Parameter | Type | Description |
---|---|---|
$key | string | the key value to set |
$value | mixed | the new value |
remove
Removes a parameter from the request.
public remove(string $key): void
Parameters
Parameter | Type | Description |
---|---|---|
$key | string | the key of the parameter to remove |
getParams
Return an array of request parameters as key/value pairs.
public getParams(?array $filter_in, ?array $filter_out): void
Parameters
Parameter | Type | Description |
---|---|---|
$filter_in | array | only include parameters with keys specified in this filter |
$filter_out | array | exclude parameters with keys specified in this filter |
hasParams
Check if the request has any parameters.
public hasParams(): bool
setParams
Sets the parameters of the request.
public setParams(array $array): void
Parameters
Parameter | Type | Description |
---|---|---|
$array | array | The array of parameters to set |
count
Returns the number of parameters in the request.
public count(): int
getMethod
public getMethod(): string
Returns the method used to initiate this request on the server. .
Generated by Hazaar API Doc Generator