(Yaf >=1.0.0)
Yaf_Route_Regex::__construct — Yaf_Route_Regex constructor
$match,$route,$map = null,$verify = null,$reverse = nullConstructs a Yaf_Route_Regex route, which routes a request URI matching a regular expression to a module, controller and action.
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.
routeWhen 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.
mapAn 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.
verifyAccepted and stored on the route, but not used: the current implementation never consults it while routing.
reverseA string, used to assemble URL, see Yaf_Route_Regex::assemble().
Note:
This parameter is introduced in 2.3.0
No value is returned.
Example #1 Yaf_Route_Regex via Yaf_Router::addRoute()
<?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
)
)
);
?>Example #3 Yaf_Route_Regex using a named capture group
<?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
)
)
);
?>Example #4 Yaf_Route_Regex via Yaf_Router::addConfig()
<?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)
);
?>