yaf框架流程三

路由的原理请看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=)
'_default' =>
object(Yaf_Route_Static)[]
'index' =>
object(Yaf_Route_Regex)[]
protected '_route' => string '#^/([a-zA-Z]+)/?#' (length=)
protected '_default' =>
array (size=)
'module' => string 'Index' (length=)
'controller' => string 'Index' (length=)
'action' => string 'Index' (length=)
protected '_maps' =>
array (size=)
=> string 'name' (length=)
protected '_verify' =>
array (size=)
'module' => string 'Index' (length=)
'controller' => string 'Index' (length=)
'action' => string 'Index' (length=)
protected '_reverse' => null
'regex' =>
object(Yaf_Route_Regex)[]
protected '_route' => string '#^list/([^/]*)/([^/]*)#' (length=)
protected '_default' =>
array (size=)
'controller' => string 'Index' (length=)
'action' => string 'action' (length=)
protected '_maps' =>
array (size=)
=> string 'name' (length=)
=> string 'value' (length=)
protected '_verify' =>
array (size=)
'controller' => string 'Index' (length=)
'action' => string 'action' (length=)
protected '_reverse' => null
'simple' =>
object(Yaf_Route_Simple)[]
protected 'controller' => string 'c' (length=)
protected 'module' => string 'm' (length=)
protected 'action' => string 'a' (length=)
'supervar' =>
object(Yaf_Route_Supervar)[]
protected '_var_name' => string 'r' (length=)
'rewrite' =>
object(Yaf_Route_Rewrite)[]
protected '_route' => string '/product/:name/:value' (length=)
protected '_default' =>
array (size=)
'controller' => string 'product' (length=)
'action' => string 'info' (length=)
protected '_verify' =>
array (size=)
'controller' => string 'product' (length=)
'action' => string 'info' (length=)
protected '_reverse' => null
'dummy' =>
object(Yaf_Route_Rewrite)[]
protected '_route' => string '/product/list/:id/:name' (length=)
protected '_default' =>
array (size=)
'controller' => string 'product' (length=)
'action' => string 'info' (length=)
protected '_verify' => null
protected '_reverse' => null

可以看出保存为一个数组,顺序与配置的顺序一致,语句最后添加的也在最后。

上一篇:网站性能延迟加载图像的五种技巧


下一篇:HTTP缓存