PHP Class 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/'.
ファイルを表示
Open project: unionofrad/lithium
Class Usage Examples
Protected Properties
Property |
Type |
Description |
|
$_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}' |
|
Public Methods
Method |
Description |
|
__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. |
|
Protected Methods
Method |
Description |
|
_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. |
|
Method Details
__construct()
public method
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_ |
return |
void |
|
_matchKeys()
protected method
A helper method used by match() to verify that options required to match this route are
present in a URL array.
protected _matchKeys ( array $options ) : mixed |
$options |
array |
An array of URL parameters. |
return |
mixed |
On success, returns an updated array of options, merged with defaults. On
failure, returns `false`. |
_matchMethod()
protected method
Helper used by Route::match() which check if the required http method is compatible
with the route.
protected _matchMethod ( array $options ) : mixed |
$options |
array |
An array of URL parameters. |
return |
mixed |
On success, returns an updated array of options, On failure, returns `false`. |
_regex()
protected method
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 `'/'`. |
return |
string |
Returns the full route template, with the value of `$token` replaced with a
generated regex capture group. |
_write()
protected method
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). |
return |
string |
Returns the route template string with option values inserted. |
canContinue()
public method
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.
Compiles URL templates into regular expression patterns for matching against request URLs,
and extracts template parameters into match-parameter arrays.
Exports the properties that make up the route to an array, for debugging, caching or
introspection purposes.
public export ( ) : array |
return |
array |
An array containing the properties of the route object, such as URL templates
and parameter lists. |
Matches a set of parameters against the route, and returns a URL string
if the route matches the parameters.
Attempts to parse a request object and determine its execution details.
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`. |
return |
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. |
Property Details
$_autoConfig protected_oe property
Auto configuration properties. Also used as the list of properties to return when exporting
this Route object to an array.
protected array $_autoConfig |
return |
array |
|
$_defaults protected_oe property
The default values for the keys present in the URL template.
protected array $_defaults |
return |
array |
|
$_handler protected_oe property
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()
));
}
});
protected callable $_handler |
return |
callable |
|
$_keys protected_oe property
An array of route parameter names (i.e. {:foo}) that appear in the URL template.
protected array $_keys |
return |
array |
|
$_match protected_oe property
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 |
return |
array |
|
$_params protected_oe property
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 |
return |
array |
|
$_pattern protected_oe property
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.
protected string $_pattern |
return |
string |
|
$_persist protected_oe property
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 |
return |
array |
|
$_subPatterns protected_oe property
An array of regular expression patterns used in route matching.
protected array $_subPatterns |
return |
array |
|
$_template protected_oe property
'/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 |
return |
string |
|