ICanBoogie
  • Documentation
  • API Reference
  • HTTP v2.5.0
Namespaces
  • ICanBoogie
    • Exception
    • HTTP
      • Headers
      • Request
      • RequestDispatcher
Classes
  • CallableDispatcher
  • File
  • FileInfo
  • FileList
  • FileResponse
  • Headers
  • RedirectResponse
  • Request
  • RequestDispatcher
  • RequestRange
  • Response
  • Status
  • WeightedDispatcher
Interfaces
  • Dispatcher
  • Exception
  • SecurityError
Exceptions
  • AuthenticationRequired
  • ClientError
  • DispatcherNotDefined
  • 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([

    'uri' => '/path/to/my/page.html?page=2',
    'user_agent' => 'Mozilla'
    'is_get' => true,
    'is_xhr' => true,
    'is_local' => true

], $_SERVER);
ICanBoogie\HTTP\Request implements ArrayAccess, IteratorAggregate uses ICanBoogie\Accessor\AccessorTrait (not available)
Namespace: ICanBoogie\HTTP
See: http://en.wikipedia.org/wiki/Uniform_resource_locator
Located at Request.php

Methods summary

protected static get_properties_mappers( void ) : Closure[]

Returns request properties mappers.

Returns request properties mappers.

Returns

Closure[]
protected static create_properties_mappers( void ) : Closure[]

Returns request properties mappers.

Returns request properties mappers.

Returns

Closure[]
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 can 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 can 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.
  • ICanBoogie\HTTP\Request::$files: a reference to the $_FILES super global array.

A request can 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 parameters are preserved:

  • path_params
  • query_params
  • request_params
  • files: The files associated with the request.
  • 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 a property that is not mapped to an environment property.

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_properties( array $properties, array $env ) : ICanBoogie\HTTP\Request

Creates an instance from an array of properties.

Creates an instance from an array of properties.

Parameters

$properties
$env

Returns

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

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

Initialize the properties ICanBoogie\HTTP\Request::$env, ICanBoogie\HTTP\Request::$headers and ICanBoogie\HTTP\Request::$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 ICanBoogie\HTTP\Request::$context, and unset ICanBoogie\HTTP\Request::$params.

Clone ICanBoogie\HTTP\Request::$headers and ICanBoogie\HTTP\Request::$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 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

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

Returns a new instance with the specified changed properties.

Returns a new instance with the specified changed properties.

Parameters

$properties

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 ICanBoogie\HTTP\Request::$env, if the key REQUEST_METHOD is not defined, the method defaults to ICanBoogie\HTTP\Request::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 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_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( void ) : ICanBoogie\HTTP\Response

connect(array $params=null)

connect(array $params=null)

Returns

ICanBoogie\HTTP\Response
public delete( void ) : ICanBoogie\HTTP\Response

delete(array $params=null)

delete(array $params=null)

Returns

ICanBoogie\HTTP\Response
public get( void ) : ICanBoogie\HTTP\Response

get(array $params=null)

get(array $params=null)

Returns

ICanBoogie\HTTP\Response
public head( void ) : ICanBoogie\HTTP\Response

head(array $params=null)

head(array $params=null)

Returns

ICanBoogie\HTTP\Response
public options( void ) : ICanBoogie\HTTP\Response

options(array $params=null)

options(array $params=null)

Returns

ICanBoogie\HTTP\Response
public post( void ) : ICanBoogie\HTTP\Response

post(array $params=null)

post(array $params=null)

Returns

ICanBoogie\HTTP\Response
public put( void ) : ICanBoogie\HTTP\Response

put(array $params=null)

put(array $params=null)

Returns

ICanBoogie\HTTP\Response
public patch( void ) : ICanBoogie\HTTP\Response

patch(array $params=null)

patch(array $params=null)

Returns

ICanBoogie\HTTP\Response
public trace( void ) : ICanBoogie\HTTP\Response

trace(array $params=null)

trace(array $params=null)

Returns

ICanBoogie\HTTP\Response

Constants summary

METHOD_ANY : string
'ANY'
METHOD_CONNECT : string
'CONNECT'
METHOD_DELETE : string
'DELETE'
METHOD_GET : string
'GET'
METHOD_HEAD : string
'HEAD'
METHOD_OPTIONS : string
'OPTIONS'
METHOD_POST : string
'POST'
METHOD_PUT : string
'PUT'
METHOD_PATCH : string
'PATCH'
METHOD_TRACE : string
'TRACE'

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

]
protected static $current_request : ICanBoogie\HTTP\Request

Current request.

Current request.


		
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.


		
protected $context : ICanBoogie\HTTP\Request\Context

General purpose container.

General purpose container.


		
public $headers : ICanBoogie\HTTP\Headers

The headers of the request.

The headers of the request.


		
protected $env : array

Request environment.

Request environment.


		
protected $files : ICanBoogie\HTTP\FileList

Files associated with the request.

Files associated with the request.


		
public $cookie

		
protected $parent : ICanBoogie\HTTP\Request

Parent request.

Parent request.


		

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_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 v2.5.0 – Check on GitHub – API documentation generated by ApiGen