yaf中路由有5种方式,这里简单实现正则路由(regex)。php
假定场景,访问yafok.com/d
,d表明数字,直接到商品详情页面.(Product模块下的product控制器下的show方法),此方法接受一个pid参数。
能够在对应控制器下经过 $paras=$this->getRequest()->getParams();
接受pid等参数bootstrap
方法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中 ,首先注册初始化配置app
public function _initConfig(Yaf_Dispatcher $dispatcher) { $this->config = Yaf_Application::app()->getConfig(); Yaf_Registry::set('config', $this->config); }
而后初始化路由this
public function _initRoute(Yaf_Dispatcher $dispatcher) { $router = $dispatcher->getInstance()->getRouter(); $router->addConfig(Yaf_Registry::get('config')->routes); }
PS:你的routes端应该和你当前的环境匹配,好比你设置为dev环境(yaf.environ=dev)
那么须要在application.ini中把routes段加上[dev :common :routes]spa
参考连接:http://php.net/manual/zh/yaf-router.addconfig.php.net
方法2:在路由初始化时,用数组添加code
$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);
以上两种方法都行,实现效果以下:router
贴一下目录blog