Yaf_Router::getCurrentRoute

(Yaf >=1.0.0)

Yaf_Router::getCurrentRoute取得當(dāng)前有效的路由名

說明

public Yaf_Router::getCurrentRoute(): string

獲取當(dāng)前路由進(jìn)程中正在起作用的路由名

注意:

你需要在路由進(jìn)程結(jié)束之后調(diào)用此方法,在這之前,這個(gè)方法會(huì)一直返回NULL

警告

本函數(shù)還未編寫文檔,僅有參數(shù)列表。

參數(shù)

此函數(shù)沒有參數(shù)。

返回值

String,當(dāng)前起效的路由的名字

范例

示例 #1 注冊(cè)一些路由到Bootstrap

<?php
class Bootstrap extends Yaf_Bootstrap_Abstract{
    public function 
_initConfig() {
        
$config Yaf_Application::app()->getConfig();
        
Yaf_Registry::set("config"$config);
    }

    public function 
_initRoute(Yaf_Dispatcher $dispatcher) {
        
$router $dispatcher->getRouter();
        
$rewrite_route  = new Yaf_Route_Rewrite(
            
"/product/list/:page",
            array(
                
"controller" => "product",
                
"action"     => "list",
            )
        ); 

        
$regex_route  = new Yaf_Route_Rewrite(
            
"#^/product/info/(\d+)",
            array(
                
"controller" => "product",
                
"action"     => "info",
            )
        ); 
        
        
$router->addRoute('rewrite'$rewrite_route)->addRoute('regex'$regex_route);
    } 

    
/**
     * register plugin 
     */
    
public function __initPlugins(Yaf_Dispatcher $dispatcher) {
        
$dispatcher->registerPlugin(new DummyPlugin());
    }
?>

示例 #2 plugin Dummy.php (under application.directory/plugins)

<?php
class DummyPlugin extends Yaf_Plugin_Abstract {

    public function 
routerShutdown(Yaf_Request_Abstract $requestYaf_Response_Abstract $response) {
         
var_dump(Yaf_Dispatcher::getInstance()->getRouter()->getCurrentRoute());
    }
?>
?>

以上例程的輸出類似于:

/* for http://yourdomain.com/product/list/1
 * DummyPlugin will output:
 */
string(7) "rewrite"

/* for http://yourdomain.com/product/info/34
 * DummyPlugin will output:
 */
string(5) "regex"

/* for other request URI
 * DummyPlugin will output:
 */
string(8) "_default"

參見