首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Yaf_Router::getCurrentRoute

(Yaf >=1.0.0)

Yaf_Router::getCurrentRoute - 获取有效的路由名称

描述

代码语言:javascript
复制
public string Yaf_Router::getCurrentRoute ( void )

获取在路线处理中有效的路线的名称。

注意:在路由过程完成之后,您应该调用此方法,因为在此之前,此方法将始终返回NULL

参数

该函数没有参数。

返回值

字符串,有效路线的名称。

示例

示例#1在Bootstrap中注册一些路由

代码语言:javascript
复制
<?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插件Dummy.php(位于 application.directory/plugins下)

代码语言:javascript
复制
<?php
class DummyPlugin extends Yaf_Plugin_Abstract {

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

上面的例子会输出类似于:

代码语言:javascript
复制
/* 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"

另请参阅

  • Yaf_Bootstrap_Abstract
  • Yaf_Plugin_Abstract
  • Yaf_Router::addRoute() - 将新路由添加到路由器中

← Yaf_Router::__construct

Yaf_Router::getRoute →

扫码关注腾讯云开发者

领取腾讯云代金券