Extends
lithium\core\StaticObject
Attempts to match an array of route parameters (i.e. `'controller'`, `'action'`, etc.)
against a connected `Route` object. For example, given the following route:
{{{
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
}}}
This will match:
{{{
$url = Router::match(array('controller' => 'users', 'action' => 'login'));
// returns /login
}}}
For URLs templates with no insert parameters (i.e. elements like `{:id}` that are replaced
with a value), all parameters must match exactly as they appear in the route parameters.
Alternatively to using a full array, you can specify routes using a more compact syntax. The
above example can be written as:
{{{ $url = Router::match('Users::login'); // still returns /login }}}
You can combine this with more complicated routes; for example:
{{{
Router::connect('/posts/{:id:\d+}', array('controller' => 'posts', 'action' => 'view'));
}}}
This will match:
{{{
$url = Router::match(array('controller' => 'posts', 'action' => 'view', 'id' => '1138'));
// returns /posts/1138
}}}
Again, you can specify the same URL with a more compact syntax, as in the following:
{{{
$url = Router::match(array('Posts::view', 'id' => '1138'));
// again, returns /posts/1138
}}}
You can use either syntax anywhere a URL is accepted, i.e.
`lithium\action\Controller::redirect()`, or `lithium\template\helper\Html::link()`.
Parameters
- string|array $url Options to match to a URL. Optionally, this can be a string containing a manually generated URL.
- object $context An instance of `lithium\action\Request`. This supplies the context for any persistent parameters, as well as the base URL for the application.
- array $options Options for the generation of the matched URL. Currently accepted values are: - `'absolute'` _boolean_: Indicates whether or not the returned URL should be an absolute path (i.e. including scheme and host name). - `'host'` _string_: If `'absolute'` is `true`, sets the host name to be used, or overrides the one provided in `$context`. - `'scheme'` _string_: If `'absolute'` is `true`, sets the URL scheme to be used, or overrides the one provided in `$context`.
Returns
string Returns a generated URL, based on the URL template of the matched route, and prefixed with the base URL of the application.Source
public static function match($url = array(), $context = null, array $options = array()) {
if (is_string($url = static::_prepareParams($url, $context, $options))) {
return $url;
}
$defaults = array('action' => 'index');
$url += $defaults;
$stack = array();
$base = isset($context) ? $context->env('base') : '';
$suffix = isset($url['#']) ? "#{$url['#']}" : null;
unset($url['#']);
foreach (static::$_configurations as $route) {
if (!$match = $route->match($url, $context)) {
continue;
}
if ($route->canContinue()) {
$stack[] = $match;
$export = $route->export();
$keys = $export['match'] + $export['keys'] + $export['defaults'];
unset($keys['args']);
$url = array_diff_key($url, $keys);
continue;
}
if ($stack) {
$stack[] = $match;
$match = static::_compileStack($stack);
}
$path = rtrim("{$base}{$match}{$suffix}", '/') ?: '/';
$path = ($options) ? static::_prefix($path, $context, $options) : $path;
return $path ?: '/';
}
$url = static::_formatError($url);
throw new RoutingException("No parameter match found for URL `{$url}`.");
}