yaf(3) 正则路由

2013年3月19日 08:39:46

废话少说,贴代码:

 1 public function _initRouter(Yaf_Dispatcher $dispatcher)
 2     {
 3         //通过派遣器得到 默认 的路由器(默认路由器是:Yaf_Router;默认路由协议是:Yaf_Rout_Static)
 4         $router = Yaf_Dispatcher::getInstance()->getRouter();
 5         $routes = array(
 6                 'test_a' => new Yaf_Route_Regex(
 7                         '#test-([a-z]+).html#',
 8                         array(
 9                                 'module' => 'test',
10                                 'controller' => 'index',
11                                 'action' => 'alpha'),
12                         array(
13                                 1 => 'data')
14                         ),
15                 'test_1' => new Yaf_Route_Regex(
16                         '#test-([0-9]+).html#',
17                         array(
18                                 'module' => 'test',
19                                 'controller' => 'index',
20                                 'action' => 'number'),
21                         array(
22                                 1 => 'data')
23                         ),
24             );
25         
26         foreach ($routes as $routekey => $route) {
27             $router->addRoute($routekey, $route);
28         }
29     }

注意:每个路由规则的正则表达式要添加限定符(分隔符,第7行和第16行的#号字符,当然也可以是其它的字母和数字之外的限定符)
yaf中的这种路由添加时好像必须是一个一个添加,所以我先定义了一个数组,然后循环添加路由规则(zf里可以直接把数组当作实参穿进去)

当然你也可以直接把路由规则写到配置文件中去

原文地址:https://www.cnblogs.com/iLoveMyD/p/2968455.html