PHP - Manual: Yaf_Route_Regex::__construct
2025-10-31
(Yaf >=1.0.0)
Yaf_Route_Regex::__construct — Yaf_Route_Regex 构造方法
$match,$route,$map = ?,$verify = ?,$reverse = ?
match
      完整的正则表达式,用来匹配请求的 uri,如果不能匹配,Yaf_Route_Regex 将返回 false。
     
route当路由正则匹配成功请求 uri 时,Yaf_Route_Regex 将会用它来决定哪一个 m/c/a 被路由。
数组中的 m/c/a 是可选的,如果没有分配特定的值,将路由到默认值。
map数组,用于为匹配结果中捕获的名称分配名称。
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
/**
    * 使用匹配结果作为 MVC 名称
    */
Yaf_Dispatcher::getInstance()->getRouter()->addRoute("name",
        new Yaf_Route_Regex(
"#^/product/([^/]+)/([^/])+#i", //match request uri leading "/product"
array(
'controller' => ":name", // 路由到 :name,匹配结果中的 $1 作为控制器名称
),
           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));
?>官方地址:https://www.php.net/manual/en/yaf-route-regex.construct.php