|
|
Yaf_Route_Regex::__construct
Yaf_Route_Regex constructor
Description
public Yaf_Route_Regex::__construct( string $match, array $route, arraynull $map = null, arraynull $verify = null, stringnull $reverse = null )
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.
-
reverse
-
A string, used to assemble URL, see
Yaf_Route_Regex::assemble.
Note:
This parameter is introduced in 2.3.0
Return Values
No value is returned.
Examples
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)
);
?>
See Also
- Yaf_Route_Regex::route
- Yaf_Route_Regex::assemble
- Yaf_Router::addRoute
- Yaf_Router::addConfig
- Yaf_Route_Static
- Yaf_Route_Supervar
- Yaf_Route_Simple
- Yaf_Route_Rewrite
- Yaf_Route_Map
|