PHP Класс lithium\net\http\Route

Typically, Route objects are created and handled through the Router class, as follows. When connecting a route, a Route object is instantiated behind the scenes, and added to the Router's collection. Router::connect("/{:controller}/{:action}"); This following matches a set of parameters against all Route objects contained in Router, and if a match is found, returns a string URL with parameters inserted into the URL pattern. Router::match(array("controller" => "users", "action" => "login")); // returns "/users/login" For more advanced routing, however, you can directly instantiate a Route object, a subclass, or any class that implements parse() and match() (see the documentation for each individual method) and configure it manually -- if, for example, you want the route to match different incoming URLs than it generates. $route = new Route(array( 'template' => '/users/{:user}', 'pattern' => '@^/u(?:sers)?(?:/(?P[^\/]+))$@', 'params' => array('controller' => 'users', 'action' => 'index'), 'match' => array('controller' => 'users', 'action' => 'index'), 'defaults' => array('controller' => 'users'), 'keys' => array('user' => 'user'), 'options' => array('compile' => false, 'wrap' => false) )); Router::connect($route); // this will match '/users/' or '/u/'.
См. также: lithium\net\http\Route::compile()
См. также: lithium\net\http\Router
Наследование: extends lithium\core\Object
Показать файл Открыть проект Примеры использования класса

Защищенные свойства (Protected)

Свойство Тип Описание
$_autoConfig array Auto configuration properties. Also used as the list of properties to return when exporting this Route object to an array.
$_defaults array The default values for the keys present in the URL template.
$_formatters array Array of closures used to format route parameters when compiling URLs.
$_handler callable new Route(array( 'template' => '/photos/{:id:[0-9]+}.jpg', 'handler' => function($request) { return new Response(array( 'headers' => array('Content-type' => 'image/jpeg'), 'body' => Photos::first($request->id)->bytes() )); } });
$_keys array An array of route parameter names (i.e. {:foo}) that appear in the URL template.
$_match array The array of values that appear in the second parameter of Router::connect(), which are **not** present in the URL template. When matching a route, these parameters must appear **exactly** as specified here.
$_meta array An array of metadata parameters which must be present in the request in order for the route to match.
$_params array An array of key/value pairs representing the parameters of the route. For keys which match parameters present in the route template, the corresponding values match the default values of those parameters. Specifying a default value for a template parameter makes that parameter optional. Any other pairs specified must match exactly when doing a reverse lookup in order for the route to match.
$_pattern string This regular expression is typically _compiled_ down from the higher-level syntax used in $_template, but can be set manually with compilation turned off in the constructor for extra control or if you are using pre-compiled Route objects.
$_persist array An array of parameter names which will persist by default when generating URLs. By default, the 'controller' parameter is set to persist, which means that the controller name matched for a given request will be used to generate all URLs for that request, unless the 'controller' parameter is specified in that URL with another value.
$_subPatterns array An array of regular expression patterns used in route matching.
$_template string '/admin/{:controller}/{:id:\d+}/{:args}'. This string can contain any combination of... 1. fixed elements, i.e. '/admin' 2. plain capture elements, i.e. '/{:controller}' 3. capture elements paired with regular expressions, i.e. '/{:id:\d+}' 4. capture elements paired with named regular expression patterns, '/{:id:ID}' 5. the speciall wildcard capture element '{:args}'

Открытые методы

Метод Описание
__construct ( array $config = [] ) : void Constructor.
canContinue ( ) : boolean Returns a boolean value indicating whether this is a continuation route. If true, this route will allow incoming requests to "fall through" to other routes, aggregating parameters for both this route and any subsequent routes.
compile ( ) : void Compiles URL templates into regular expression patterns for matching against request URLs, and extracts template parameters into match-parameter arrays.
export ( ) : array Exports the properties that make up the route to an array, for debugging, caching or introspection purposes.
match ( array $options = [] ) : string | boolean Matches a set of parameters against the route, and returns a URL string if the route matches the parameters.
parse ( Request $request, array $options = [] ) : object | boolean Attempts to parse a request object and determine its execution details.

Защищенные методы

Метод Описание
_init ( )
_matchKeys ( array $options ) : mixed A helper method used by match() to verify that options required to match this route are present in a URL array.
_matchMethod ( array $options ) : mixed Helper used by Route::match() which check if the required http method is compatible with the route.
_regex ( string $regex, string $param, string $token, string $prefix ) : string Generates a sub-expression capture group for a route regex, using an optional user-supplied matching pattern.
_write ( array $options, array $defaults ) : string Writes a set of URL options to this route's template string.

Описание методов

__construct() публичный Метод

Constructor.
public __construct ( array $config = [] ) : void
$config array Available configuration options are: - `'params'` _array_ - `'template'` _string_ - `'pattern'` _string_ - `'match'` _array_ - `'meta'` _array_ - `'defaults'` _array_ - `'keys'` _array_ - `'persist'` _array_ - `'handler'` _callable_ - `'continue'` _boolean_ - `'modifiers'` _array_ - `'formatters'` _array_ - `'unicode'` _boolean_
Результат void

_init() защищенный Метод

protected _init ( )

_matchKeys() защищенный Метод

A helper method used by match() to verify that options required to match this route are present in a URL array.
См. также: lithium\net\http\Route::match()
protected _matchKeys ( array $options ) : mixed
$options array An array of URL parameters.
Результат mixed On success, returns an updated array of options, merged with defaults. On failure, returns `false`.

_matchMethod() защищенный Метод

Helper used by Route::match() which check if the required http method is compatible with the route.
См. также: lithium\net\http\Route::match()
protected _matchMethod ( array $options ) : mixed
$options array An array of URL parameters.
Результат mixed On success, returns an updated array of options, On failure, returns `false`.

_regex() защищенный Метод

Generates a sub-expression capture group for a route regex, using an optional user-supplied matching pattern.
protected _regex ( string $regex, string $param, string $token, string $prefix ) : string
$regex string An optional user-supplied match pattern. If a route is defined like `"/{:id:\d+}"`, then the value will be `"\d+"`.
$param string The parameter name which the capture group is assigned to, i.e. `'controller'`, `'id'` or `'args'`.
$token string The full token representing a matched element in a route template, i.e. `'/{:action}'`, `'/{:path:js|css}'`, or `'.{:type}'`.
$prefix string The prefix character that separates the parameter from the other elements of the route. Usually `'.'` or `'/'`.
Результат string Returns the full route template, with the value of `$token` replaced with a generated regex capture group.

_write() защищенный Метод

Writes a set of URL options to this route's template string.
protected _write ( array $options, array $defaults ) : string
$options array The options to write to this route, with defaults pre-merged.
$defaults array The default template options for this route (contains hard-coded default values).
Результат string Returns the route template string with option values inserted.

canContinue() публичный Метод

Returns a boolean value indicating whether this is a continuation route. If true, this route will allow incoming requests to "fall through" to other routes, aggregating parameters for both this route and any subsequent routes.
public canContinue ( ) : boolean
Результат boolean Returns the value of `$_config['continue']`.

compile() публичный Метод

Compiles URL templates into regular expression patterns for matching against request URLs, and extracts template parameters into match-parameter arrays.
public compile ( ) : void
Результат void

export() публичный Метод

Exports the properties that make up the route to an array, for debugging, caching or introspection purposes.
public export ( ) : array
Результат array An array containing the properties of the route object, such as URL templates and parameter lists.

match() публичный Метод

Matches a set of parameters against the route, and returns a URL string if the route matches the parameters.
public match ( array $options = [] ) : string | boolean
$options array An array of parameters.
Результат string | boolean URL string on success, else `false` if the route didn't match.

parse() публичный Метод

Attempts to parse a request object and determine its execution details.
См. также: lithium\net\http\Request
См. также: lithium\net\http\Request::$params
См. также: lithium\net\http\Route::$_handler
public parse ( Request $request, array $options = [] ) : object | boolean
$request Request A request object containing the details of the request to be routed.
$options array Used to determine the operation of the method, and override certain values in the `Request` object: - `'url'` _string_: If present, will be used to match in place of the `$url` property of `$request`.
Результат object | boolean If this route matches `$request`, returns the request with execution details attached to it (inside `Request::$params`). Alternatively when a route handler function was used, returns the result of its invocation. Returns `false` if the route never matched.

Описание свойств

$_autoConfig защищенное свойство

Auto configuration properties. Also used as the list of properties to return when exporting this Route object to an array.
См. также: lithium\net\http\Route::export()
protected array $_autoConfig
Результат array

$_defaults защищенное свойство

The default values for the keys present in the URL template.
См. также: lithium\net\http\Route::$_template
См. также: lithium\net\http\Route::$_keys
protected array $_defaults
Результат array

$_formatters защищенное свойство

Array of closures used to format route parameters when compiling URLs.
См. также: lithium\net\http\Router::formatters()
protected array $_formatters
Результат array

$_handler защищенное свойство

new Route(array( 'template' => '/photos/{:id:[0-9]+}.jpg', 'handler' => function($request) { return new Response(array( 'headers' => array('Content-type' => 'image/jpeg'), 'body' => Photos::first($request->id)->bytes() )); } });
См. также: lithium\net\http\Route::parse()
См. также: lithium\net\http\Response
protected callable $_handler
Результат callable

$_keys защищенное свойство

An array of route parameter names (i.e. {:foo}) that appear in the URL template.
См. также: lithium\net\http\Route::$_template
protected array $_keys
Результат array

$_match защищенное свойство

The array of values that appear in the second parameter of Router::connect(), which are **not** present in the URL template. When matching a route, these parameters must appear **exactly** as specified here.
protected array $_match
Результат array

$_meta защищенное свойство

An array of metadata parameters which must be present in the request in order for the route to match.
protected array $_meta
Результат array

$_params защищенное свойство

An array of key/value pairs representing the parameters of the route. For keys which match parameters present in the route template, the corresponding values match the default values of those parameters. Specifying a default value for a template parameter makes that parameter optional. Any other pairs specified must match exactly when doing a reverse lookup in order for the route to match.
protected array $_params
Результат array

$_pattern защищенное свойство

This regular expression is typically _compiled_ down from the higher-level syntax used in $_template, but can be set manually with compilation turned off in the constructor for extra control or if you are using pre-compiled Route objects.
См. также: lithium\net\http\Route::$_template
См. также: lithium\net\http\Route::__construct()
protected string $_pattern
Результат string

$_persist защищенное свойство

An array of parameter names which will persist by default when generating URLs. By default, the 'controller' parameter is set to persist, which means that the controller name matched for a given request will be used to generate all URLs for that request, unless the 'controller' parameter is specified in that URL with another value.
protected array $_persist
Результат array

$_subPatterns защищенное свойство

An array of regular expression patterns used in route matching.
protected array $_subPatterns
Результат array

$_template защищенное свойство

'/admin/{:controller}/{:id:\d+}/{:args}'. This string can contain any combination of... 1. fixed elements, i.e. '/admin' 2. plain capture elements, i.e. '/{:controller}' 3. capture elements paired with regular expressions, i.e. '/{:id:\d+}' 4. capture elements paired with named regular expression patterns, '/{:id:ID}' 5. the speciall wildcard capture element '{:args}'
protected string $_template
Результат string