Yaf_Route_Regex::__construct

(Yaf >=1.0.0)

Yaf_Route_Regex::__constructThe __construct purpose

說明

publicYaf_Route_Regex::__construct(
    string $match,
    array $route,
    array $map = ?,
    array $verify = ?
)

參數(shù)

match

一個完整的正則表達式,用來匹配一個請求的uri,如果不能匹配,Yaf_Route_Regex 將返回FALSE。

route

當路由正則匹配成功請求uri時,Yaf_Route_Regex將會用它來決定哪一個m/c/a被路由。

在這個數(shù)組中無論是m/c/a都是最優(yōu)的,如果你沒有提供一個明確的值,它將會以默認方式被路由。 另外, 你也可以使用map的結(jié)果作為m/c/a的結(jié)果.

map

將匹配到的結(jié)果捕捉放到一個已經(jīng)命名好的數(shù)組中。

verify

返回值

范例

示例 #1 Yaf_Route_Regex()example

<?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(
              
=> "name",   // now you can call $request->getParam("name")
              
=> "id",     // to get the first captrue in the match pattern.
           
)
        )
    );
?>

示例 #2 Yaf_Route_Regex(as of 2.3.0)()example

<?php
   
/**
    *  使用動態(tài)的controller
    */
    
Yaf_Dispatcher::getInstance()->getRouter()->addRoute("name",
        new 
Yaf_Route_Regex(
           
"#^/product/([^/]+)/([^/])+#"//match request uri leading "/product"
           
array(
              
'controller' => ":name",  //使用上面匹配的:name, 也就是$1作為controller
           
),
           array(
              
=> "name",   // now you can call $request->getParam("name")
              
=> "id",     // to get the first captrue in the match pattern.
           
)
        )
    );
?>

示例 #3 Yaf_Route_Regex()example

<?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(
              
=> "uri",   // now you can call $request->getParam("uri")
           
),
        ),
    );
    
Yaf_Dispatcher::getInstance()->getRouter()->addConfig(
        new 
Yaf_Config_Simple($config));
?>

參見