Yaf 默认路由模式:
Yaf_Route_Simple
Yaf_Route_Supervar
Yaf_Route_Static
Yaf_Route_Map
Yaf_Route_Rewrite
Yaf_Route_Regex
路由顺序加载,如果匹配上第一条就不向下匹配了。
1. 默认协议路由
默认路由的匹配模式是这样如下这样的:
/:modules/:controller:/:action/:params
会按照上边的顺序匹配,但是这里有一点注意的,当你访问 /admin/index/index 会解析成如下这样
array(
'module' => 'admin',
'controller' => 'index',
'action' => 'index',
)
但是,如果是 /admin/index 缺少了 Action 参数,系统会先判断 Action,所以它就把 admin 判断成了 Controller,index 判断成了 Action 。
2. 加载路由
一种是ini配置文件的方式,被解析成数组然后加载的
注册路由,在 Bootstrap 中写一个 _init 开头的方法:
public function _initRoute(Yaf_Dispatcher $dispatcher) {
$router = Yaf_Dispatcher::getInstance()->getRouter();
$router->addConfig(Yaf_Registry::get("config")->routes);
}
上边的代码是读取的Ini 文件中的配置,我们可以这样加载,譬如我们要使用这种格式 index.php?m=admin&c=index&a=index ,就可以在里边添加我们的路由,代码如下:
$route = new Yaf_Route_Simple("m", "c", "a");
$router->addRoute("name", $route);
一种是直接写在 bootsrap里面。
具体请参考官网:http://www.php.net/manual/en/book.yaf.php
比如官网的例子:http://php.net/manual/en/yaf-router.addroute.php
getConfig(); Yaf_Registry::set("config", $config); } public function _initRoute(Yaf_Dispatcher $dispatcher) { $router = $dispatcher->getRouter(); /** * we can add some pre-defined routes in application.ini */ $router->addConfig(Yaf_Registry::get("config")->routes); /** * add a Rewrite route, then for a request uri: * http://***/product/list/22/foo * will be matched by this route, and result: * * [module] => * [controller] => product * [action] => info * [method] => GET * [params:protected] => Array * ( * [id] => 22 * [name] => foo * ) * */ $route = new Yaf_Route_Rewrite( "/product/list/:id/:name", array( "controller" => "product", "action" => "info", ) ); $router->addRoute('dummy', $route); } ?>