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行):

 public function setRoute()
{
$this->route_config = array(
'subdomain' => array(),
'uri' => array()
); //================子域名路由
$this->route_config['subdomain']['#love_(\d)_([a-z])#'] = 'test/index/safe/$1/$2';
$this->route_config['subdomain']['#love_(\d)#'] = 'test/index/safe/$1';
$this->route_config['subdomain']['#love#'] = 'bbs/index/index'; //================ uri路由
$this->route_config['uri']['#([a-z]+)_(\d+)\.html#'] = 'test/index/testroute/$1/$2';
$this->route_config['uri']['#map\.html#'] = 'map/index/line';
}

测试结果:

 http://love_1.zhangzhibin.com/
Array
(
[$1] => 1
) http://love_1_a.zhangzhibin.com/
Array
(
[$1] => 1
[$2] => a
)

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

整体的逻辑是这样的

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

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

下边贴出代码:

 <?php
class Route
{
public $subdomain = '';
public $uri = ''; public $ismatch = false;
public $DomainMatchName = false;
public $UriMatchName = false;
public $MatchName = false; public $module = 'index';
public $controller = 'index';
public $action = 'index'; public $args = array(); public $error = ''; public $route_config = array(
'subdomain' => array(),
'uri' => array()
); public function __construct($subdomain, $uri)
{
$this->subdomain = $subdomain;
$this->uri = $uri; //设置路由配置信息
$this->setRoute(); //先检查子域名路由
//如果子域名中只有英文字母就不再匹配
if (!empty($this->route_config['subdomain']) || !ctype_alpha($this->subdomain)) {
$this->DomainMatchName = $this->preg_parse($this->route_config['subdomain'], $this->subdomain);
} //再检查uri路由
//如果uri中不含有"/"才会走正则匹配
if (!empty($this->route_config['uri']) && strpos($this->uri, '/') === false) {
$this->UriMatchName = $this->preg_parse($this->route_config['uri'], $this->uri);
} else {
$this->arr_parse();
} ($this->DomainMatchName || $this->UriMatchName) && ($this->ismatch = true); $this->MatchName = $this->UriMatchName ? $this->UriMatchName : $this->DomainMatchName; return $this;
} public function setRoute()
{
//================子域名路由
$this->route_config['subdomain']['#love_(\d)_([a-z])#'] = 'test/index/safe/$1/$2';
$this->route_config['subdomain']['#love_(\d)#'] = 'test/index/safe/$1';
$this->route_config['subdomain']['#love#'] = 'bbs/index/index'; //================ uri路由
$this->route_config['uri']['#svnsort#'] = 'index/index/svnsort';
$this->route_config['uri']['#nicename_json#'] = 'index/index/nicenamejson';
$this->route_config['uri']['#nicename#'] = 'index/index/nicename';
$this->route_config['uri']['#\btest\b[^\/]#'] = 'test/index/rtest'; //是下边路由的前缀, 加上\b就是全词匹配了
$this->route_config['uri']['#\btest1\b#'] = 'test/index/rtest'; // 不会匹配test, 只会匹配test1 } //正则匹配分析
public function preg_parse($routeconfig, $subject)
{
$routename = false;
foreach ($routeconfig as $pattern => $route) {
$arrRoute = explode('/', $route); preg_match($pattern, $subject, $matches); if (empty($matches)) {
continue;
} else {
$routename = $pattern; $arrMCA = array_slice($arrRoute, 0, 3);
$arrArg = array_slice($arrRoute, 3); $this->module = $arrMCA[0];
$this->controller = $arrMCA[1];
$this->action = $arrMCA[2]; $arrMatchArg = array();
foreach ($matches as $key => $value) {
$arrMatchArg['$'.$key] = $value;
} foreach ($arrArg as $value) {
$this->args[$value] = $arrMatchArg[$value];
} break;
}
} return $routename;
} //正则检查uri没有匹配上的情况下, 数组匹配分析
public function arr_parse()
{
//获得module/controller/action
$arrPathInfo = explode('/', $this->uri);//存放URL中以正斜线隔开的内容的数组 !empty($arrPathInfo[0]) && ($this->module = $arrPathInfo[0]);
!empty($arrPathInfo[1]) && ($this->controller = $arrPathInfo[1]);
!empty($arrPathInfo[2]) && ($this->action = $arrPathInfo[2]); //存放除module/controller/action之外的参数
// /a/1/b/2/c/3 ==> ?a=1&b=2&c=3
// 当键和值不成对出现时,默认最后一个键的值为0
// 参数中不要出现数字键,否则在合并post,get参数时会出错
$intPathInfo = count($arrPathInfo);
if ($intPathInfo > 3) {
$arrPath = array_slice($arrPathInfo, 3);
$intArgNum = count($arrPath);
if ($intArgNum % 2 != 0) {
$arrPath[] = 0;
}
$intArgNum = count($arrPath); for ($i=0; $i<$intArgNum; $i=$i+2) {
$this->args[$arrPath[$i]] = $arrPath[$i+1];
}
}
}
}

当然你也可以写自己的路由类, 只需要返回给框架所需要的数据就行啦~

     //可以自己实现路由类, 只要要返回 module, controller, action, 以及匹配到的参数就行了
//返回是否匹配了路由, 匹配路由的名字, 路由指向的module,controller,action, 路由错误信息,方便调试
public function route()
{
$this->route = new Route($this->subdomain, $this->document_uri); $this->isRouteMatch = $this->route->ismatch;
$this->RouteMatchName = $this->route->MatchName; $this->module = $this->route->module;
$this->controller = $this->route->controller;
$this->action = $this->route->action; $this->setData();
}

zpf 路由功能的更多相关文章

  1. 吐血原创:mini2440和win7笔记本利用无路由功能的交换机共享上网(使用x-router软路由)

    真的是要吐血了,为了使自己的win7系统笔记本和mini2440,通过交换机(没有路由功能,才5口,和HUB差不多)共享宽带上网,并且连接上的宽带还是长城宽带,我用尽各种cmd命令都查不到长城宽带的默 ...

  2. PHP实现一个简单url路由功能

    如果一个页面的内容呈现,需要根据url上传递的参数来进行渲染.很多时候可能是这样子写:xxx.com/xx?c=x&m=x& t=..,而我们看到的url往往是这样子的(以新浪微游戏的 ...

  3. 玩转nodeJS系列:使用原生API实现简单灵活高效的路由功能(支持nodeJs单机集群),nodeJS本就应该这样轻快

    前言: 使用nodeJS原生API实现快速灵活路由,方便与其他库/框架进行整合: 1.原生API,简洁高效的轻度封装,加速路由解析,nodeJS本就应该这样轻快 2.不包含任何第三方库/框架,可以灵活 ...

  4. nodeJS实现路由功能

    前面的话 本文将使用NodeJS实现较复杂应用的路由功能 结构 项目结构如下 代码如下 功能 [router.js] // 加载所需模块 var http = require('http'); var ...

  5. 理解 angular 的路由功能

    相信很多人使用angular 都是因为他路由功能而用的 深入理解ANGULARUI路由_UI-ROUTER 最近在用 ionic写个webapp 看到几个demo中路由有好几种,搞的有点晕,查下资料研 ...

  6. Cisco 的基本配置实例之五----交换机的路由功能与DHCP 功能

    5.配置交换机的路由功能 说明:只有在三层交换机上才有路由功能,其他的二层接入交换机要想在不同的vlan之间传送数据需要通过trunk口到核心交换机上进行完路由交换后才可以. TEST(config) ...

  7. Android 的媒体路由功能应用与框架解析

    一.功能描述 Android 的媒体路由API被设计用来允许多种媒体(视频.音乐.图片)在与ANDROID设备连接(无线或有线)的辅助设备(如电视.立体声.家庭戏院系统.音乐播放机)上显示和播放,使用 ...

  8. SpringCloud系列八:Zuul 路由访问(Zuul 的基本使用、Zuul 路由功能、zuul 过滤访问、Zuul 服务降级)

    1.概念:Zuul 路由访问 2.具体内容 在现在为止所有的微服务都是通过 Eureka 找到的,但是在很多的开发之中为了规范微服务的使用,提供有一个路由的处理控制组件:Zuul,也就是说 Zuul ...

  9. 【学习笔记】node.js重构路由功能

    摘要:利用node.js模块化实现路由功能,将请求路径作为参数传递给一个route函数,这个函数会根据参数调用一个方法,最后输出浏览器响应内容 1.介绍 node.js是一个基于Chrome V8引擎 ...

随机推荐

  1. Java7的异常处理新特性-addSuppressed()方法等

    开发人员对异常处理的try-catch-finally语句块都比较熟悉.如果在try语句块中抛出了异常,在控制权转移到调用栈上一层代码之前,finally语句块中的语句也会执行.但是finally语句 ...

  2. jQuery 遍历 - find() 方法

    实例 搜索所有段落中的后代 span 元素,并将其颜色设置为红色: $("p").find("span").css('color','red'); 详情:htt ...

  3. C#反射机制(转)

    一:反射的定义 审查元数据并收集关于它的类型信息的能力.元数据(编译以后的最基本数据单元)就是一大堆的表,当编译程序集或者模块时,编译器会创建一个类定义表,一个字段定义表,和一个方法定义表等. Sys ...

  4. python 运行时报错误SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 2

    File "1.py", line 2SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 2, but no ...

  5. readline

    注意,向后表示向左,向前表示向右. "\C-f": forward-char 光标向右一个字符 "\C-b": backward-char  光标向左一个字符 ...

  6. Eclipse构建Maven项目

    1. 安装m2eclipse插件     要用Eclipse构建Maven项目,我们需要先安装meeclipse插件     点击eclipse菜单栏Help->Eclipse Marketpl ...

  7. Java基础相关总结

    临近面试,权当复习了吧 final相关 定义常量的方法  eg:final int i=0;//则i不能被修改 final修饰的类不能被继承,因此没有子类,且它的类中的方法默认是final final ...

  8. jQuery回调、递延对象总结(中篇) —— 神奇的then方法

    前言: 什么叫做递延对象,生成一个递延对象只需调用jQuery.Deferred函数,deferred这个单词译为延期,推迟,即延迟的意思,那么在jQuery中 又是如何表达延迟的呢,从递延对象中的t ...

  9. IE打开报错,提示该内存不能为read的解决办法!

    由于最近我遇到过一次浏览器打不开的情况,出错的错误提示为 浏览器错误:“0x5ddfddac”指令引用的“0x00000020”内存,该内存不能为read经过杀毒及IE修复均不能解决!(没试过360急 ...

  10. USB协议[转]_基本上涵盖了所有最基础的USB协议相关知识。

    背景: 需要使用到USB协议,我一直尝试着去强记这个流程,现在看来,其实不用.看多了,把这个过程具象出来,就牢牢记住了. 正文: 正文转自:http://fangjian0518.blog.163.c ...