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. Java实现zip压缩多个文件下载

    为了更好的演示,首先创建一个文件实体FileBean,包含了文件路径和文件名称: package com.javaweb.entity; import java.io.Serializable; /* ...

  2. webpack构建与loaders

    loaders 定义 先了解一下webpack,webpack是一个用于针对js文件的构建工具,在被构建的js文件中,我们可以使用require语句和webpack loader,如下: var cs ...

  3. HDU 1048

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> int main() { char ...

  4. R语言练习(二)

    op <- par(mfrow = c(2, 2)) #设置画布 p2 <- curve(x^2, 0, 1) #绘制曲线 legend("topleft", inse ...

  5. 求方程式ax^2+bx+c=0的根。

    #include <stdio.h>#include <stdlib.h>#include<math.h>int main(){ int a,b,c,d; doub ...

  6. 【PHP面向对象(OOP)编程入门教程】2.什么是类,什么是对象,类和对象这间的关系

    类的概念:类是具有相同属性和服务的一组对象的集合.它为属于该类的所有对象提供了统一的抽象描述,其内部包括属性和服务两个主要部分.在面向对象的编程语言中,类是一个独立的程序单位,它应该有一个类名并包括属 ...

  7. git之remote repository create(远程仓库创建)

    参考:Git教程 - 廖雪峰的官方网站 1.在Git bash窗口执行如下指令创建SSH KEY: ssh-keygen -t rsa -C "sunjf@biomarker.com.cn& ...

  8. php中向mysql插入数据

     $sql='insert into news(title,subtitle,source,publishtime,content,image,author) values("'.unico ...

  9. C 文件读写 容易疏忽的一个问题

    今天需要解决一个问题,将影像瓦片(一堆jpg文件)分别进行读取,并将所有数据以文件流的方式存入一个.db的文件中, 同时将每个jpg数据在db文件中的位置保存下来,作为index存在.idx文件中. ...

  10. 比较两个数据库表table结构不同之处

    /*--比较两个数据库的表字段差异 hy 适用多种版本库 --*/ /*--调用示例 exec p_comparestructure 'database1','database2' --*/ ) dr ...