lithium\net\http\Router::connect()
Connects a new route and returns the current routes array. This method creates a new
Route
object and registers it with the Router
. The order in which routes are connected
matters, since the order of precedence is taken into account in parsing and matching
operations.
A callable can be passed in place of $options
. In this case the callable acts as a route
handler. Route handlers should return an instance of lithium\net\http\Response
and can be used to short-circuit the framework's lookup and invocation of controller
actions:
Router::connect('/photos/{:id:[0-9]+}.jpg', [], function($request) {
return new Response([
'headers' => ['Content-type' => 'image/jpeg'],
'body' => Photos::first($request->id)->bytes()
]);
});
Parameters
-
string|object
$template
An empty string, a route string
/
or an instance oflithium\net\http\Route
. -
array|string
$params
An array describing the default or required elements of the route or alternatively a path string i.e.
Posts::index
. -
array|callable
$options
Either an array of options (
'handler'
,'formatters'
,'modifiers'
,'unicode'
as well as any options forRoute
) or a callable that will be used as a route handler.
Returns
\lithium\net\http\RouteInstance of the connected route.
Source
public static function connect($template, $params = [], $options = []) {
if (is_array($options) && isset($options['scope'])) {
$name = $options['scope'];
} else {
$name = static::$_scope;
}
if (is_object($template)) {
return (static::$_configurations[$name][] = $template);
}
if (is_string($params)) {
$params = static::_parseString($params, false);
}
if (isset($params[0]) && is_array($tmp = static::_parseString($params[0], false))) {
unset($params[0]);
$params = $tmp + $params;
}
$params = static::_parseController($params);
if (is_callable($options)) {
$options = ['handler' => $options];
}
$config = compact('template', 'params') + $options + [
'formatters' => static::formatters(),
'modifiers' => static::modifiers(),
'unicode' => static::$_unicode
];
return (static::$_configurations[$name][] = static::_instance('route', $config));
}