Yaf_Route_Regex::__construct
Yaf_Route_Regex::__construct
(Yaf >=1.0.0)
Yaf_Route_Regex::__construct - Yaf_Route_Regex构造函数
描述
public Yaf_Route_Regex::__construct ( string $match , array $route [, array $map [, array $verify [, string $reverse ]]] )
参数
match
完整的正则表达式模式将用于匹配请求的URI,如果不匹配,Yaf_Route_Regex将返回FALSE
。
route
当匹配模式匹配请求uri时,Yaf_Route_Regex将使用它来决定要发送哪个m/c/a。
这个数组中的m/c/a中的任何一个都是optianl,如果你没有指定一个特定的值,它将被路由到默认值。
map
一个数组,用于为匹配结果中的captrues分配名称。
verify
reverse
一个字符串,用于组装url,请参阅Yaf_Route_Regex::assemble()。
注意
:该参数在2.3.0中介绍
返回值
示例
示例#1 Yaf_Route_Regex()示例
<?php
/**
* Add a regex route to Yaf_Router route stack
*/
Yaf_Dispatcher::getInstance()->getRouter()->addRoute("name",
new Yaf_Route_Regex(
"#^/product/([^/]+)/([^/])+#", //match request uri leading "/product"
array(
'controller' => "product", //route to product controller,
),
array(
1 => "name", // now you can call $request->getParam("name")
2 => "id", // to get the first captrue in the match pattern.
)
)
?>
示例#2 Yaf_Route_Regex(从2.3.0开始)()示例
<?php
/**
* Use match result as MVC name
*/
Yaf_Dispatcher::getInstance()->getRouter()->addRoute("name",
new Yaf_Route_Regex(
"#^/product/([^/]+)/([^/])+#i", //match request uri leading "/product"
array(
'controller' => ":name", // route to :name, which is $1 in the match result as controller name
),
array(
1 => "name", // now you can call $request->getParam("name")
2 => "id", // to get the first captrue in the match pattern.
)
)
?>
示例#3 Yaf_Route_Regex和命名捕获地(从2.3.0开始)()示例
<?php
/**
* Use match result as MVC name
*/
Yaf_Dispatcher::getInstance()->getRouter()->addRoute("name",
new Yaf_Route_Regex(
"#^/product/(?<name>[^/]+)/([^/])+#i", //match request uri leading "/product"
array(
'controller' => ":name", // route to :name,
// which is named capture group 'name' in the match result as controller name
),
array(
2 => "id", // to get the first captrue in the match pattern.
)
)
?>
示例#4 Yaf_Route_Regex()示例
<?php
/**
* Add a regex route to Yaf_Router route stack by calling addconfig
*/
$config = array(
"name" => array(
"type" => "regex", //Yaf_Route_Regex route
"match" => "#(.*)#", //match arbitrary request uri
"route" => array(
'controller' => "product", //route to product controller,
'action' => "dummy", //route to dummy action
),
"map" => array(
1 => "uri", // now you can call $request->getParam("uri")
),
),
Yaf_Dispatcher::getInstance()->getRouter()->addConfig(
new Yaf_Config_Simple($config)
?>
另请参阅
- Yaf_Router::addRoute() - 将新路由添加到路由器中
- Yaf_Router::addConfig() - 将配置定义的路由添加到路由器中
- Yaf_Route_Static
- Yaf_Route_Supervar
- Yaf_Route_Simple
- Yaf_Route_Rewrite
- Yaf_Route_Map
← Yaf_Route_Regex::assemble
Yaf_Route_Regex::route →