路由的原理请看http://yaf.laruence.com/manual/yaf.routes.html这个链接
要点:路由的顺序是堆栈模式的,及最后添加的路由规则最优先。由上两篇可知,定义的第一条路由是application实例化时候的new Yaf_Route_Static()。
然后在bootstrap里有初始化路由的操作,主要方法是:
1.从配置添加
$routes = $this->config->routes;
$router = $dispatcher->getRouter();
$router->addConfig($routes);
2.语句添加
$route = new Yaf_Route_Rewrite(
“/product/list/:id/:name”,
array(
“controller” => “product”,
“action” => “info”,
)
);
$router->addRoute(‘dummy’, $route);
通过$dispatcher->getRouter()->getRoutes()可以查看到
array (size=7) '_default' => object(Yaf_Route_Static)[6] 'index' => object(Yaf_Route_Regex)[12] protected '_route' => string '#^/([a-zA-Z]+)/?#' (length=17) protected '_default' => array (size=3) 'module' => string 'Index' (length=5) 'controller' => string 'Index' (length=5) 'action' => string 'Index' (length=5) protected '_maps' => array (size=1) 1 => string 'name' (length=4) protected '_verify' => array (size=3) 'module' => string 'Index' (length=5) 'controller' => string 'Index' (length=5) 'action' => string 'Index' (length=5) protected '_reverse' => null 'regex' => object(Yaf_Route_Regex)[13] protected '_route' => string '#^list/([^/]*)/([^/]*)#' (length=23) protected '_default' => array (size=2) 'controller' => string 'Index' (length=5) 'action' => string 'action' (length=6) protected '_maps' => array (size=2) 1 => string 'name' (length=4) 2 => string 'value' (length=5) protected '_verify' => array (size=2) 'controller' => string 'Index' (length=5) 'action' => string 'action' (length=6) protected '_reverse' => null 'simple' => object(Yaf_Route_Simple)[14] protected 'controller' => string 'c' (length=1) protected 'module' => string 'm' (length=1) protected 'action' => string 'a' (length=1) 'supervar' => object(Yaf_Route_Supervar)[15] protected '_var_name' => string 'r' (length=1) 'rewrite' => object(Yaf_Route_Rewrite)[16] protected '_route' => string '/product/:name/:value' (length=21) protected '_default' => array (size=2) 'controller' => string 'product' (length=7) 'action' => string 'info' (length=4) protected '_verify' => array (size=2) 'controller' => string 'product' (length=7) 'action' => string 'info' (length=4) protected '_reverse' => null 'dummy' => object(Yaf_Route_Rewrite)[17] protected '_route' => string '/product/list/:id/:name' (length=23) protected '_default' => array (size=2) 'controller' => string 'product' (length=7) 'action' => string 'info' (length=4) protected '_verify' => null protected '_reverse' => null
可以看出保存为一个数组,顺序与配置的顺序一致,语句最后添加的也在最后。