码迷,mamicode.com
首页 > 其他好文 > 详细

zpf 路由功能

时间:2015-04-11 22:29:42      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

2015-4-11 20:51:06

又搞了一天, 解决了一堆bug, 重新规划了类文件夹, 改善自动加载功能

最新的特性就是支持子域名路由了

因为整个框架还在完善当中, 而且里边有好多接口有我自己申请的第三方appkey 和 secretkey, 所以还不方便提供下载

但是路由功能完成以后,整体的框架也就快完工了, 再修修补补就行了

四年啦, 从零开始, 终于有模有样了....

比如: 

访问 www.zhangzhibin.com 是会跳转到网站首页的

访问 www.zhangzhibin.com/bbs 是会跳转到bbs论坛页面

但是我想在访问 love.zhangzhibin.com时也跳转到bbs论坛页面 (因为在大众点评留下的开发者信息是love.zhangzhibin.com)

此时可以在路由文件中设置一个对应关系就可以了(第8行):

 1 public function setRoute()
 2     {
 3         $this->route_config = array(
 4             ‘subdomain‘ => array(),
 5             ‘uri‘ => array());
 6 
 7         //================子域名路由
 8         $this->route_config[‘subdomain‘][‘#love#‘] = ‘bbs/index/index‘;
 9         //================ uri路由
10         $this->route_config[‘uri‘][‘#([a-z]+)_(\d+)\.html#‘] = ‘test/index/testroute/$1/$2‘;
11         $this->route_config[‘uri‘][‘#map\.html#‘] = ‘map/index/line‘;
12     }

注意, 这里的路由项的键两边有两个‘#‘做为边界符是需要自己去写的, 如果担心冲突, 大家可以根据需要自己去设定边界符

整体的逻辑是这样的

先匹配子域名的路由, 然后匹配uri的路由(不匹配?后边的查询串)

将两步匹配到的参数合并起来, 因为uri在第二步匹配, 所以uri的匹配结果会覆盖掉子域名的路由结果

下边贴出代码: (跳出循环匹配的时机还有待优化...)

 1 <?php
 2 class Route
 3 {
 4     public $routename = ‘‘;
 5 
 6     public $route_config = array();
 7 
 8     public $ismatch = false;
 9 
10     public $module = ‘‘;
11     public $controller = ‘‘;
12     public $action = ‘‘;
13 
14     public $args = array();
15 
16     public $subdomain = ‘‘;
17     public $uri = ‘‘;
18 
19     public $error = ‘‘;
20 
21     public function __construct($subdomain, $uri)
22     {
23         $this->subdomain = $subdomain;
24         $this->uri = $uri;
25 
26         //设置路由配置信息
27         $this->setRoute();
28 
29         //先检查子域名路由
30         $this->parse_subdomain();
31 
32         //再检查uri路由
33         $this->parse_uri();
34 
35         return $this;
36     }
37 
38     public function setRoute()
39     {
40         $this->route_config = array(
41             ‘subdomain‘ => array(),
42             ‘uri‘ => array());
43 
44         //================子域名路由
45         $this->route_config[‘subdomain‘][‘#love#‘] = ‘bbs/index/index‘;
46         //================ uri路由
47         $this->route_config[‘uri‘][‘#([a-z]+)_(\d+)\.html#‘] = ‘test/index/testroute/$1/$2‘;
48         $this->route_config[‘uri‘][‘#map\.html#‘] = ‘map/index/line‘;
49     }
50 
51     public function parse_subdomain()
52     {
53         $this->subdomain && $this->parse($this->route_config[‘subdomain‘], $this->subdomain);
54     }
55 
56     public function parse_uri()
57     {
58         $this->uri && $this->parse($this->route_config[‘uri‘], $this->uri);
59     }
60 
61     public function parse($routeconfig, $subject)
62     {
63         foreach ($routeconfig as $pattern => $route) {
64             $arrRoute = explode(‘/‘, $route);
65             
66             // if (count($arrRoute) < 3) {
67             //     $this->error = $pattern.‘: 未指明module/controller/action/$1/$2...‘;
68             // }
69             
70             // preg_match_all($pattern, $uri, $matches);
71             preg_match($pattern, $subject, $matches);
72 
73             if (empty($matches)) {
74                 continue;
75             } else {
76                 $arrMCA = array_slice($arrRoute, 0, 3);
77                 $arrArg = array_slice($arrRoute, 3);
78 
79                 $this->routename = $pattern;
80 
81                 $this->module = $arrMCA[0];
82                 $this->controller = $arrMCA[1];
83                 $this->action = $arrMCA[2];
84 
85                 $arrMatchArg = array();
86                 foreach ($matches as $key => $value) {
87                     $arrMatchArg[‘$‘.$key] = $value;
88                 }
89 
90                 foreach ($arrArg as $value) {
91                     $this->args[$value] = $arrMatchArg[$value];
92                 }
93 
94                 $this->ismatch = true;
95                 break;
96             }
97         }
98     }
99 }

 

zpf 路由功能

标签:

原文地址:http://www.cnblogs.com/iLoveMyD/p/4418417.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!