ICanBoogie
  • Documentation
  • API Reference
  • HTTP 3.0.x
Namespaces
  • ICanBoogie
    • Exception
    • HTTP
      • Headers
      • Request
      • RequestDispatcher
Classes
  • CallableDispatcher
  • DispatcherProvider
  • File
  • FileInfo
  • FileList
  • FileResponse
  • Headers
  • ProvideDispatcher
  • RedirectResponse
  • Request
  • RequestDispatcher
  • RequestOptionsMapper
  • RequestRange
  • Response
  • Status
  • WeightedDispatcher
Interfaces
  • Dispatcher
  • Exception
  • FileOptions
  • RequestMethods
  • RequestOptions
  • ResponseStatus
  • SecurityError
Exceptions
  • AuthenticationRequired
  • ClientError
  • DispatcherNotDefined
  • DispatcherProviderNotDefined
  • ForceRedirect
  • MethodNotSupported
  • NotFound
  • PermissionRequired
  • ServerError
  • ServiceUnavailable
  • StatusCodeNotValid

Class Request

An HTTP request.

<?php

use ICanBoogie\HTTP\Request;

# Creating the main request

$request = Request::from($_SERVER);

# Creating a request from scratch, with the current environment.

$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

ICanBoogie\HTTP\Request
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:

  • ICanBoogie\HTTP\Request::$cookie: a reference to the $_COOKIE super global array.
  • ICanBoogie\HTTP\Request::$path_params: initialized to an empty array.
  • ICanBoogie\HTTP\Request::$query_params: a reference to the $_GET super global array.
  • ICanBoogie\HTTP\Request::$request_params: a reference to the $_POST super global array.
  • $files: a reference to the $_FILES super global array.

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

ICanBoogie\HTTP\Request

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

ICanBoogie\HTTP\Request
protected static from_uri( string $uri, array $env ) : ICanBoogie\HTTP\Request

Creates an instance from an URI.

Creates an instance from an URI.

Parameters

$uri
$env

Returns

ICanBoogie\HTTP\Request
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

$options
$env

Returns

ICanBoogie\HTTP\Request
protected static get_options_mapper( void ) : ICanBoogie\HTTP\RequestOptionsMapper

Returns

ICanBoogie\HTTP\RequestOptionsMapper
protected __construct( array $properties, array $env = [] )

Initialize the properties $env, ICanBoogie\HTTP\Request::$headers and $context.

Initialize the properties $env, ICanBoogie\HTTP\Request::$headers and $context.

If the ICanBoogie\HTTP\Request::$params property is null it is set with an union of ICanBoogie\HTTP\Request::$path_params, ICanBoogie\HTTP\Request::$request_params and ICanBoogie\HTTP\Request::$query_params.

Parameters

$properties
Initial properties.
$env
Environment of the request, usually the $_SERVER super global.

Throws

ICanBoogie\HTTP\MethodNotSupported
when the request method is not supported.
public __clone( void )

Clone ICanBoogie\HTTP\Request::$headers and $context, and unset ICanBoogie\HTTP\Request::$params.

Clone ICanBoogie\HTTP\Request::$headers and $context, and unset ICanBoogie\HTTP\Request::$params.

public __invoke( void ) : ICanBoogie\HTTP\Response

Alias for ICanBoogie\HTTP\Request::send().

Alias for ICanBoogie\HTTP\Request::send().

Returns

ICanBoogie\HTTP\Response
The response to the request.
public send( string|null $method = null, array $params = null ) : ICanBoogie\HTTP\Response

Dispatch the request.

Dispatch the request.

The parent property is used for request chaining.

Note: If an exception is thrown during dispatch $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

$method
Use this parameter to change the request method.
$params

Use this parameter to change the ICanBoogie\HTTP\Request::$request_params property of the request.

Returns

ICanBoogie\HTTP\Response
The response to the request.

Throws

Exception
re-throws exception raised during dispatch.
protected dispatch( void ) : ICanBoogie\HTTP\Response

Dispatches the request using the ICanBoogie\HTTP\Request::dispatch() helper.

Dispatches the request using the ICanBoogie\HTTP\Request::dispatch() helper.

Returns

ICanBoogie\HTTP\Response
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

$options

Returns

ICanBoogie\HTTP\Request

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

$method
$arguments

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

$param

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

ICanBoogie\HTTP\Request
protected get_context( void ) : ICanBoogie\HTTP\Request\Context

Returns the request's context.

Returns the request's context.

Returns

ICanBoogie\HTTP\Request\Context
protected get_cache_control( void ) : ICanBoogie\HTTP\Headers\CacheControl

Returns the Cache-Control header.

Returns the Cache-Control header.

Returns

ICanBoogie\HTTP\Headers\CacheControl
protected get_script_name( void ) : string

Returns the script name.

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.

Returns the request method.

This is the getter for the method magic property.

The method is retrieved from $env, if the key REQUEST_METHOD is not defined, the method defaults to ICanBoogie\HTTP\RequestMethods::METHOD_GET.

Returns

string
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 $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 $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 $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 $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

Returns the union of the ICanBoogie\HTTP\Request::$path_params, ICanBoogie\HTTP\Request::$request_params and ICanBoogie\HTTP\Request::$query_params properties.

Returns the union of the ICanBoogie\HTTP\Request::$path_params, ICanBoogie\HTTP\Request::$request_params and ICanBoogie\HTTP\Request::$query_params properties.

This method is the getter of the ICanBoogie\HTTP\Request::$params magic property.

Returns

array

Magic methods summary

public connect( array $params = null ) : ICanBoogie\HTTP\Response

Parameters

$params

Returns

ICanBoogie\HTTP\Response
public delete( array $params = null ) : ICanBoogie\HTTP\Response

Parameters

$params

Returns

ICanBoogie\HTTP\Response
public get( array $params = null ) : ICanBoogie\HTTP\Response

Parameters

$params

Returns

ICanBoogie\HTTP\Response
public head( array $params = null ) : ICanBoogie\HTTP\Response

Parameters

$params

Returns

ICanBoogie\HTTP\Response
public options( array $params = null ) : ICanBoogie\HTTP\Response

Parameters

$params

Returns

ICanBoogie\HTTP\Response
public post( array $params = null ) : ICanBoogie\HTTP\Response

Parameters

$params

Returns

ICanBoogie\HTTP\Response
public put( array $params = null ) : ICanBoogie\HTTP\Response

Parameters

$params

Returns

ICanBoogie\HTTP\Response
public patch( array $params = null ) : ICanBoogie\HTTP\Response

Parameters

$params

Returns

ICanBoogie\HTTP\Response
public trace( array $params = null ) : ICanBoogie\HTTP\Response

Parameters

$params

Returns

ICanBoogie\HTTP\Response

Constants summary

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]
SAFE_METHODS : array
[self::METHOD_DELETE,self::METHOD_PATCH,self::METHOD_POST,self::METHOD_PUT]

Constants inherited from ICanBoogie\HTTP\RequestMethods

METHOD_ANY, METHOD_CONNECT, METHOD_DELETE, METHOD_GET, METHOD_HEAD, METHOD_OPTIONS, METHOD_PATCH, METHOD_POST, METHOD_PUT, METHOD_TRACE

Constants inherited from ICanBoogie\HTTP\RequestOptions

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 $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.

[]
public $params : array

Union of ICanBoogie\HTTP\Request::$path_params, ICanBoogie\HTTP\Request::$request_params and ICanBoogie\HTTP\Request::$query_params.

Union of ICanBoogie\HTTP\Request::$path_params, ICanBoogie\HTTP\Request::$request_params and ICanBoogie\HTTP\Request::$query_params.


		
public $headers : ICanBoogie\HTTP\Headers

The headers of the request.

The headers of the request.


		
public $cookie

		

Magic properties

public read-only $context : ICanBoogie\HTTP\Request\Context

the request's context.

public read-only $parent : ICanBoogie\HTTP\Request

Parent request.

public read-only $files : ICanBoogie\HTTP\FileList

The files associated with the request.

public read-only $authorization : boolean

Authorization of the request.

public read-only $content_length : integer

Length of the request content.

public read-only $cache_control : ICanBoogie\HTTP\Headers\CacheControl
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

Is this a GET request?

public read-only $is_head : boolean

Is this a HEAD request?

public read-only $is_options : boolean

Is this a OPTIONS request?

public read-only $is_patch : boolean

Is this a PATCH request?

public read-only $is_post : boolean

Is this a POST request?

public read-only $is_put : boolean

Is this a PUT request?

public read-only $is_trace : boolean

Is this a TRACE request?

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

Is this a local request?

public read-only $is_xhr : boolean

Is this an Ajax request?

public read-only $method : string

Method of the request.

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

Port of the request.

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

Referer of the request.

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.

HTTP 3.0.x – Check on GitHub – API documentation generated by ApiGen