$Request
- is system object, that provides unified source of information about request, instance can be obtained in such way:
$Request = \cs\Request::instance();
$Request
object has next public method:
- init()
- init_from_globals()
- init_server()
- init_query()
- init_data_and_files()
- init_cookie()
- init_route()
- header()
- query()
- data()
- files()
- cookie()
- route()
- route_path()
- route_ids()
- analyze_route_path()
init($server : string[], $query : array, $data : array, $files : array[], $data_stream : null|resource|string, $cookie : string[], $request_started :: float)
Initialize request with specified data, internally increases counter in static property ::$request_id
and calls consequently ::init_server()
, :init_query()
, ::init_data_adn_files()
, ::init_cookie()
and ::init_route()
Initialize request object from superglobals $_SERVER
, $_GET
, $_POST
, $_COOKIE
and $_FILES
(including parsing php://input
when necessary), just wrapper around ::init()
Initialize server configuration (including request headers), $server
structure is the same as $_SERVER
Initialize query array, $query
structure is the same as $_GET
init_data_and_files($data = [] : array, $files = [] : array[], $data_stream = null : null|resource|string, $copy_stream = true : bool)
Initialize request data and files, $data
structure is the same as $_POST
, $files
structure is either the same as $_FILES
or normalized (where $_FILES['file'][0]['name']
instead of $_FILES['file']['name'][0]
)
Initialize cookies array, $cookie
structure is the same as $_COOKIE
Initializes route, required to be called after ::init_server()
Get header by name, if header not present - returns false
Get query parameter by name, if parameter (or least one in case of many) not present - returns null
Get data item by name, if item (or least one in case of many) not present - returns null
Get file item by name
Get cookie by name
Get route part by index
Get route path part by index
Get route ids part by index
As result returns current route in system in form of array, normalized path, detects module path points to, whether this is API call, administration page, or home page
Example of returned data:
{
"route" : [
"general"
],
"path_normalized" : "admin/System/general",
"admin_path" : true,
"api_path" : false,
"cli_path" : false,
"current_module" : "System",
"home_page" : false
}
$Request
object has next public properties:
- id
- started
- method
- host
- scheme
- secure
- protocol
- path
- uri
- query_string
- remote_addr
- ip
- headers
- query
- data
- files
- data_stream
- cookie
- mirror_index
- path_normalized
- route
- route_path
- route_ids
- admin_path
- api_path
- cli_path
- regular_path
- current_module
- home_page
Global request id, used by system
Unix timestamp when request processing started
Uppercase method, GET by default
The best guessed host
Schema http
or https
Is requested with HTTPS
Protocol, for instance: HTTP/1.0
, HTTP/1.1
(default), HTTP/2.0
Path
URI, basically $path?$query_string
(without ?
is query string is empty), /
by default
Query string
Where request came from, not necessary real IP of client, 127.0.0.1
by default
The best guessed IP of client (based on all known headers), $this->remote_addr
by default
Headers are normalized to lowercase keys with hyphen as separator, for instance: connection
, referer
, content-type
, accept-language
Query array, similar to $_GET
Data array, similar to $_POST
Normalized files array
Each file item can be either single file or array of files (in contrast with native PHP arrays where each field like name
become an array) with keys name
, type
, size
, tmp_name
, stream
and error
.
name
, type
, size
and error
keys are similar to native PHP fields in $_FILES
; tmp_name
might not be temporary file, but file descriptor wrapper like request-file:///file
instead and stream
is resource like obtained with fopen('/tmp/xyz', 'rb')
.
Data stream resource, similar to fopen('php://input', 'rb')
Cookie array, similar to $_COOKIE
, but also contains un-prefixed keys according to system configuration
Current mirror according to configuration
Normalized processed representation of relative address, may differ from raw, should be used in most cases
Contains parsed route of current page url in form of array without module name and prefixes admin|api
.
For page admin/System/general/system
:
[
"general",
"system"
]
Like $route
property, but excludes numerical items
Like $route
property, but only contains numerical items (opposite to route_path property)
Request to administration section
Request to api section
Request to cli interface
Request to regular page (not administration section, not API and not CLI)
Current module
Home page
###[Up](#) Events$Request object supports next events:
- System/Request/routing_replace/before
- System/Request/routing_replace/after
This event is used by components in order to change current route. Array with one element rc
is set as parameter for event, is contains reference to string of current route. It may be changed by components, for example, to catch request to some page, that actually doesn't exists, and to make some replacements here.
[
'rc' => &$rc //Reference to string with current route, this string can be changed
]
Similar to System/Request/routing_replace/before
, but happens after language, module, home page and other things are identified and removed from $rc
.
[
'route' => &$route, // Reference to an array with current route, if changed without changing `$route_path` and
// `$route_ids`, they will be automatically updated as well
'route_path' => &$route_path,
'route_ids' => &$route_ids,
'cli_path' => &$cli_path,
'admin_path' => &$admin_path,
'api_path' => &$api_path,
'regular_path' => &$regular_path,
'current_module' => &$current_module,
'home_page' => &$home_page
]