Constructs a Yaf_Route_Regex route, which routes a
request URI matching a regular expression to a module, controller and action.
Parameters
match
A complete regular expression pattern, used to match the request URI.
When it does not match, Yaf_Route_Regex::route()
returns false and the router moves on to the next route.
route
When the pattern matches the request URI, this array determines the
module, controller and action to route to.
Each of the three entries is optional; when one is not given, the
default is used. An entry whose value starts with :
is resolved against the parameters produced by
map.
map
An array mapping capture group indexes to parameter names. Only the
captures listed here become request parameters; a numeric capture that
is not listed is discarded.
Named capture groups are always added under their own name and do not
need to be listed.
verify
Accepted and stored on the route, but not used: the current
implementation never consults it while routing.
<?php
/**
* Add a regex route to the Yaf_Router route stack
*/
Yaf_Dispatcher::getInstance()->getRouter()->addRoute(
"name",
new Yaf_Route_Regex(
"#^/product/([^/]+)/([^/]+)#", // match a URI leading "/product"
array(
'controller' => "product", // route to the product controller
),
array(
1 => "name", // $request->getParam("name") holds the first capture
2 => "id", // and $request->getParam("id") the second
)
)
);
?>
Example #2 Yaf_Route_Regex using a capture as the controller name
<?php
/**
* Use a capture as the controller name
*/
Yaf_Dispatcher::getInstance()->getRouter()->addRoute(
"name",
new Yaf_Route_Regex(
"#^/product/([^/]+)/([^/]+)#i", // match a URI leading "/product"
array(
'controller' => ":name", // the "name" capture (see map) becomes the controller
),
array(
1 => "name", // $request->getParam("name") holds the first capture
2 => "id", // and $request->getParam("id") the second
)
)
);
?>
<?php
/**
* Use a named capture group as the controller name
*/
Yaf_Dispatcher::getInstance()->getRouter()->addRoute(
"name",
new Yaf_Route_Regex(
"#^/product/(?<name>[^/]+)/([^/]+)#i", // match a URI leading "/product"
array(
'controller' => ":name", // the "name" capture becomes the controller
),
array(
2 => "id", // group 1 is named, only group 2 needs a map entry
)
)
);
?>
<?php
/**
* Add a regex route to the Yaf_Router route stack through a config array
*/
$config = array(
"name" => array(
"type" => "regex", // a Yaf_Route_Regex route
"match" => "#(.*)#", // match any request URI
"route" => array(
'controller' => "product", // route to the product controller
'action' => "dummy", // and to the dummy action
),
"map" => array(
1 => "uri", // $request->getParam("uri") holds the matched URI
),
),
);
Yaf_Dispatcher::getInstance()->getRouter()->addConfig(
new Yaf_Config_Simple($config)
);
?>