Class Request
An HTTP request.
<?php
use ICanBoogie\HTTP\Request;
$request = Request::from($_SERVER);
$request = Request::from([
Request::OPTION_URI => '/path/to/my/page.html?page=2',
Request::OPTION_USER_AGENT => 'Mozilla'
Request::OPTION_IS_GET => true,
Request::OPTION_IS_XHR => true,
Request::OPTION_IS_LOCAL => true
], $_SERVER);
-
ICanBoogie\HTTP\Request
implements
ArrayAccess,
IteratorAggregate,
ICanBoogie\HTTP\RequestMethods,
ICanBoogie\HTTP\RequestOptions
uses
ICanBoogie\Accessor\AccessorTrait (not available)
Namespace: ICanBoogie\
HTTP
See:
http://en.wikipedia.org/wiki/Uniform_resource_locator
Located at Request.php
Methods summary
public static
get_current_request( void )
: ICanBoogie\HTTP\Request
Returns the current request.
Returns the current request.
Returns
protected
get_files( void )
public static
from( array $properties
= null, array $env
= [] )
: ICanBoogie\HTTP\Request
A request may be created from the $_SERVER
super global array. In that case $_SERVER
is
used as environment the request is created with the following properties:
A request may be created from the $_SERVER
super global array. In that case $_SERVER
is
used as environment the request is created with the following properties:
A request may also be created from an array of properties, in which case most of them are
mapped to the $env
constructor param. For instance, is_xhr
set the
HTTP_X_REQUESTED_WITH
environment property to 'XMLHttpRequest'. In fact, only the
following options are preserved:
- Request::OPTION_PATH_PARAMS
- Request::OPTION_QUERY_PARAMS
- Request::OPTION_REQUEST_PARAMS
- Request::OPTION_FILES: The files associated with the request.
- Request::OPTION_HEADERS: The header fields of the request. If specified, the headers
available in the environment are ignored.
Parameters
$properties
- Properties of the request.
$env
- Environment, usually the
$_SERVER
array.
Returns
Throws
InvalidArgumentException
in attempt to use an unsupported option.
protected static
from_server( void )
: ICanBoogie\HTTP\Request
Creates an instance from the $_SERVER
array.
Creates an instance from the $_SERVER
array.
Returns
protected static
from_uri( string $uri
, array $env
)
: ICanBoogie\HTTP\Request
Creates an instance from an URI.
Creates an instance from an URI.
Parameters
Returns
protected static
from_options( array $options
, array $env
)
: ICanBoogie\HTTP\Request
Creates an instance from an array of properties.
Creates an instance from an array of properties.
Parameters
Returns
protected
__construct( array $properties
, array $env
= [] )
public
send( string|null $method
= null, array $params
= null )
: ICanBoogie\HTTP\Response
Dispatch the request.
The ICanBoogie\HTTP\Request::$parent
property is used for request chaining.
Note: If an exception is thrown during dispatch ICanBoogie\HTTP\Request::$current_request
is not updated!
Note: If the request is changed because of the $method
or $params
parameters, it
is the changed instance that is dispatched, not the actual instance.
Parameters
Returns
Throws
Exception
re-throws exception raised during dispatch.
public
with( array $options
)
: ICanBoogie\HTTP\Request
Returns a new instance with the specified changed properties.
Returns a new instance with the specified changed properties.
Parameters
Returns
Throws
InvalidArgumentException
protected
adapt( string $method
, array $params
= null )
: ICanBoogie\HTTP\Request
Adapts the request to the specified method and params.
Adapts the request to the specified method and params.
Parameters
$method
- The method.
$params
- The params.
Returns
ICanBoogie\HTTP\Request
The same instance is returned if the method is the same and the params
are null
. Otherwise a changed request is returned.
public
__call( $method
, $arguments
)
: mixed
Overrides the method to provide a virtual method for each request method.
Overrides the method to provide a virtual method for each request method.
Example:
<?php
Request::from('/api/core/aloha')->get();
Parameters
Returns
mixed
public
offsetExists( string $param
)
: boolean
Checks if the specified param exists in the request's params.
Checks if the specified param exists in the request's params.
Parameters
$param
- The name of the parameter.
Returns
boolean
Implementation of
ArrayAccess::offsetExists()
public
offsetGet( string $param
)
: mixed|null
Get the specified param from the request's params.
Get the specified param from the request's params.
Parameters
$param
- The name of the parameter.
Returns
mixed|null
The value of the parameter, or null
if the parameter does not exists.
Implementation of
ArrayAccess::offsetGet()
public
offsetSet( string $param
, mixed $value
)
Set the specified param to the specified value.
Set the specified param to the specified value.
Parameters
$param
- The name of the parameter.
$value
- The value of the parameter.
Implementation of
ArrayAccess::offsetSet()
public
offsetUnset( mixed $param
)
Remove the specified param from the request's parameters.
Remove the specified param from the request's parameters.
Parameters
Implementation of
ArrayAccess::offsetUnset()
public
getIterator( void )
: ArrayIterator
Returns an array iterator for the params.
Returns an array iterator for the params.
Returns
ArrayIterator
Implementation of
IteratorAggregate::getIterator()
protected
get_parent( void )
: ICanBoogie\HTTP\Request
Returns the parent request.
Returns the parent request.
Returns
protected
get_context( void )
: ICanBoogie\HTTP\Request\Context
Returns the request's context.
Returns the request's context.
Returns
protected
get_script_name( void )
: string
Returns the script name.
The setter is volatile, the value is returned from the ENV key SCRIPT_NAME
.
Returns
string
protected
get_method( void )
: string
Returns the request method.
protected
get_query_string( void )
: string|null
Returns the query string of the request.
Returns the query string of the request.
The value is obtained from the QUERY_STRING
key of the ICanBoogie\HTTP\Request::$env
array.
Returns
string|null
protected
get_content_length( void )
: integer|null
Returns the content length of the request.
Returns the content length of the request.
The value is obtained from the CONTENT_LENGTH
key of the ICanBoogie\HTTP\Request::$env
array.
Returns
integer|null
protected
get_referer( void )
: string|null
Returns the referer of the request.
Returns the referer of the request.
The value is obtained from the HTTP_REFERER
key of the ICanBoogie\HTTP\Request::$env
array.
Returns
string|null
protected
get_user_agent( void )
: string|null
Returns the user agent of the request.
Returns the user agent of the request.
The value is obtained from the HTTP_USER_AGENT
key of the ICanBoogie\HTTP\Request::$env
array.
Returns
string|null
protected
get_is_delete( void )
: boolean
Checks if the request method is DELETE
.
Checks if the request method is DELETE
.
Returns
boolean
protected
get_is_get( void )
: boolean
Checks if the request method is GET
.
Checks if the request method is GET
.
Returns
boolean
protected
get_is_head( void )
: boolean
Checks if the request method is HEAD
.
Checks if the request method is HEAD
.
Returns
boolean
protected
get_is_options( void )
: boolean
Checks if the request method is OPTIONS
.
Checks if the request method is OPTIONS
.
Returns
boolean
protected
get_is_patch( void )
: boolean
Checks if the request method is PATCH
.
Checks if the request method is PATCH
.
Returns
boolean
protected
get_is_post( void )
: boolean
Checks if the request method is POST
.
Checks if the request method is POST
.
Returns
boolean
protected
get_is_put( void )
: boolean
Checks if the request method is PUT
.
Checks if the request method is PUT
.
Returns
boolean
protected
get_is_trace( void )
: boolean
Checks if the request method is TRACE
.
Checks if the request method is TRACE
.
Returns
boolean
protected
get_is_idempotent( void )
: boolean
Whether the request method is idempotent.
Whether the request method is idempotent.
Returns
boolean
See
http://restcookbook.com/HTTP%20Methods/idempotency/
protected
get_is_safe( void )
: boolean
Whether the request method is safe.
Whether the request method is safe.
Returns
boolean
See
http://restcookbook.com/HTTP%20Methods/idempotency/
protected
get_is_xhr( void )
: boolean
Checks if the request is a XMLHTTPRequest
.
Checks if the request is a XMLHTTPRequest
.
Returns
boolean
protected
get_is_local( void )
: boolean
Checks if the request is local.
Checks if the request is local.
Returns
boolean
protected
get_ip( void )
: string
Returns the remote IP of the request.
Returns the remote IP of the request.
If defined, the HTTP_X_FORWARDED_FOR
header is used to retrieve the original IP.
If the REMOTE_ADDR
header is empty the request is considered local thus ::1
is returned.
Returns
string
See
http://en.wikipedia.org/wiki/X-Forwarded-For
protected
get_authorization( void )
protected
get_uri( void )
: string
Returns the REQUEST_URI
environment key.
Returns the REQUEST_URI
environment key.
If the REQUEST_URI
key is not defined by the environment, the value is fetched from
the $_SERVER
array. If the key is not defined in the $_SERVER
array null
is returned.
Returns
string
protected
get_port( void )
: integer
Returns the port of the request.
Returns the port of the request.
Returns
integer
protected
get_path( void )
: string
Returns the path of the request, that is the REQUEST_URI
without the query string.
Returns the path of the request, that is the REQUEST_URI
without the query string.
Returns
string
protected
get_normalized_path( void )
: string
Returns the $path property normalized using the
\ICanBoogie\normalize_url_path() function.
Returns the $path property normalized using the
\ICanBoogie\normalize_url_path() function.
Returns
string
protected
get_extension( void )
: mixed
Returns the extension of the path info.
Returns the extension of the path info.
Returns
mixed
protected
lazy_set_params( $params
)
protected
lazy_get_params( void )
: array
Magic methods summary
Constants summary
METHOD_ANY
,
METHOD_CONNECT
,
METHOD_DELETE
,
METHOD_GET
,
METHOD_HEAD
,
METHOD_OPTIONS
,
METHOD_PATCH
,
METHOD_POST
,
METHOD_PUT
,
METHOD_TRACE
OPTION_CACHE_CONTROL
,
OPTION_CONTENT_LENGTH
,
OPTION_COOKIE
,
OPTION_FILES
,
OPTION_HEADERS
,
OPTION_IP
,
OPTION_IS_CONNECT
,
OPTION_IS_DELETE
,
OPTION_IS_GET
,
OPTION_IS_HEAD
,
OPTION_IS_LOCAL
,
OPTION_IS_OPTIONS
,
OPTION_IS_PATCH
,
OPTION_IS_POST
,
OPTION_IS_PUT
,
OPTION_IS_TRACE
,
OPTION_IS_XHR
,
OPTION_METHOD
,
OPTION_PATH
,
OPTION_PATH_PARAMS
,
OPTION_QUERY_PARAMS
,
OPTION_REFERER
,
OPTION_REQUEST_PARAMS
,
OPTION_URI
,
OPTION_USER_AGENT
Properties summary
public static
$methods
: array
[
self::METHOD_CONNECT,
self::METHOD_DELETE,
self::METHOD_GET,
self::METHOD_HEAD,
self::METHOD_OPTIONS,
self::METHOD_POST,
self::METHOD_PUT,
self::METHOD_PATCH,
self::METHOD_TRACE
]
public
$path_params
: array
Parameters extracted from the request path.
Parameters extracted from the request path.
[]
public
$query_params
: array
Parameters defined by the query string.
Parameters defined by the query string.
[]
public
$request_params
: array
Parameters defined by the request body.
Parameters defined by the request body.
[]
protected
$context
: ICanBoogie\HTTP\Request\Context
General purpose container.
General purpose container.
Magic properties
public read-only
$authorization
: boolean
Authorization of the request.
public read-only
$content_length
: integer
Length of the request content.
public read-only
$ip
: string
Remote IP of the request.
public read-only
$is_delete
: boolean
Is this a DELETE
request?
public read-only
$is_get
: boolean
public read-only
$is_head
: boolean
public read-only
$is_options
: boolean
Is this a OPTIONS
request?
public read-only
$is_patch
: boolean
public read-only
$is_post
: boolean
public read-only
$is_put
: boolean
public read-only
$is_trace
: boolean
public read-only
$is_safe
: boolean
Is the request method considered safe?
public read-only
$is_idempotent
: boolean
Is the request method considered idempotent?
public read-only
$is_local
: boolean
public read-only
$is_xhr
: boolean
public read-only
$method
: string
public read-only
$normalized_path
: string
Path of the request normalized using the \ICanBoogie\normalize_url_path() function.
public read-only
$path
: string
Path info of the request.
public read-only
$extension
: string
The extension of the path.
public read-only
$port
: integer
public read-only
$query_string
: string
Query string of the request.
public read-only
$script_name
: string
Name of the entered script.
public read-only
$referer
: string
public read-only
$user_agent
: string
User agent of the request.
public read-only
$uri
: string
URI of the request. The QUERY_STRING
value of the environment is overwritten when the instance is created with the $uri property.