yaf自定义正则路由的两种方式
发布时间:2020-12-14 00:56:05 所属栏目:百科 来源:网络整理
导读:yaf中路由有5种方式,这里简单实现正则路由(regex)。 假定场景,访问 yafok.com/d ,d代表数字,直接到商品详情页面.(Product模块下的product控制器下的show方法),此方法接受一个pid参数。 可以在对应控制器下通过 $paras=$this-getRequest()-getParams();
yaf中路由有5种方式,这里简单实现正则路由(regex)。 假定场景,访问 方法1:application.ini中添加 [routes] routes.product.type='regex' routes.product.match='#^/([0-9]+)[/]?$#' routes.product.route.module='Product' routes.product.route.controller='product' routes.product.route.action='show' routes.product.map.1='pid' bootstrap.php中,首先注册初始化配置 public function _initConfig(Yaf_Dispatcher $dispatcher) { $this->config = Yaf_Application::app()->getConfig(); Yaf_Registry::set('config',$this->config); } 然后初始化路由 public function _initRoute(Yaf_Dispatcher $dispatcher) { $router = $dispatcher->getInstance()->getRouter(); $router->addConfig(Yaf_Registry::get('config')->routes); } PS:你的routes端应该和你当前的环境匹配,比如你设置为dev环境(yaf.environ=dev) 参考链接:http://php.net/manual/zh/yaf-router.addconfig.php 方法2:在路由初始化时,用数组添加 $router = $dispatcher->getInstance()->getRouter(); $route = new Yaf_Route_Regex( '/([0-9]+)/',array('module'=>'Product','controller' => 'product','action' => 'show' ),array("1"=>'pid') ); $router->addRoute('product',$route); 以上两种方法都行,实现效果如下:
贴一下目录 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |