一个简单的php路由类


Posted in PHP onMay 29, 2016

本文实例为大家分享了php编写一个简单的路由类,供大家参考,具体内容如下

<?php
namespace cmhc\Hcrail;
 
class Hcrail
{
 
  /**
   * callback function
   * @var callable
   */
  protected static $callback;
 
  /**
   * match string or match regexp
   * @var string
   */
  protected static $match;
 
  protected static $routeFound = false;
 
  /**
   * deal with get,post,head,put,delete,options,head
   * @param  $method
   * @param  $arguments
   * @return
   */
  public static function __callstatic($method, $arguments)
  {
    self::$match = str_replace("//", "/", dirname($_SERVER['PHP_SELF']) . '/' . $arguments[0]);
    self::$callback = $arguments[1];
    self::dispatch();
    return;
  }
 
  /**
   * processing ordinary route matches
   * @param string $requestUri
   * @return
   */
  public static function normalMatch($requestUri)
  {
    if (self::$match == $requestUri) {
      self::$routeFound = true;
      call_user_func(self::$callback);
    }
    return;
  }
 
  /**
   * processing regular route matches
   * @param string $requestUri
   * @return
   */
  public static function regexpMatch($requestUri)
  {
    //处理正则表达式
    $regexp = self::$match;
    preg_match("#$regexp#", $requestUri, $matches);
    if (!empty($matches)) {
      self::$routeFound = true;
      call_user_func(self::$callback, $matches);
    }
    return;
  }
 
  /**
   * dispatch route
   * @return
   */
  public static function dispatch()
  {
    if (self::$routeFound) {
      return ;
    }
    $requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    $requestMethod = $_SERVER['REQUEST_METHOD'];
 
    if (strpos(self::$match, '(') === false) {
      self::normalMatch($requestUri);
    } else {
      self::regexpMatch($requestUri);
    }
 
  }
 
  /**
   * Determining whether the route is found
   * @return boolean
   */
  public static function isNotFound()
  {
    return !self::$routeFound;
  }
 
}

下载地址:https://github.com/cmhc/Hcrail

希望本文所述对大家学习PHP程序设计有所帮助。

PHP 相关文章推荐
PHP数据缓存技术
Feb 14 PHP
隐性调用php程序的方法
Mar 09 PHP
全世界最小的php网页木马一枚 附PHP木马的防范方法
Oct 09 PHP
编写Smarty插件在模板中直接加载数据的详细介绍
Jun 26 PHP
PHP引用符&amp;的用法详细解析
Aug 22 PHP
Thinkphp模板中截取字符串函数简介
Jun 17 PHP
php中用memcached实现页面防刷新功能
Aug 19 PHP
PHP自定session保存路径及删除、注销与写入的方法
Nov 18 PHP
ioncube_loader_win_5.2.dll的错误解决方法
Jan 04 PHP
浅析Yii2集成富文本编辑器redactor实例教程
Apr 25 PHP
PHP对象克隆clone用法示例
Sep 28 PHP
thinkPHP框架通过Redis实现增删改查操作的方法详解
May 13 PHP
解读PHP中上传文件的处理问题
May 29 #PHP
round robin权重轮循算法php实现代码
May 28 #PHP
php自定义中文字符串截取函数substr_for_gb2312及substr_for_utf8示例
May 28 #PHP
php获取一定范围内取N个不重复的随机数
May 28 #PHP
smarty的section嵌套循环用法示例
May 28 #PHP
PHP引用返回用法示例
May 28 #PHP
php时间函数用法分析
May 28 #PHP
You might like
PHP UTF8编码内的繁简转换类
2009/07/20 PHP
php读取纯真ip数据库使用示例
2014/01/26 PHP
Apache PHP MySql安装配置图文教程
2016/08/27 PHP
PHP编程快速实现数组去重的方法详解
2017/07/22 PHP
Laravel学习教程之IOC容器的介绍与用例
2017/08/15 PHP
Ubuntu上安装yaf扩展的方法
2018/01/29 PHP
php解决安全问题的方法实例
2019/09/19 PHP
jquery 多行滚动代码(附详细解释)
2010/06/17 Javascript
如何实现textarea里的不同文本显示不同颜色
2014/01/20 Javascript
开发中可能会用到的jQuery小技巧
2014/03/07 Javascript
jQuery实现高亮显示网页关键词的方法
2015/08/07 Javascript
javascript针对cookie的基本操作实例详解
2015/11/30 Javascript
JS封装的选项卡TAB切换效果示例
2016/09/20 Javascript
JavaScript基于面向对象实现的猜拳游戏
2018/01/03 Javascript
简单理解Vue中的nextTick方法
2018/01/30 Javascript
详解mpvue scroll-view自动回弹bug解决方案
2018/10/01 Javascript
jQuery内容选择器与表单选择器实例分析
2019/06/28 jQuery
linux 下以二进制的方式安装 nodejs
2020/02/12 NodeJs
jquery实现简单拖拽效果
2020/07/20 jQuery
urllib和BeautifulSoup爬取维基百科的词条简单实例
2018/01/17 Python
python中partial()基础用法说明
2018/12/30 Python
漂亮的Django Markdown富文本app插件的实现
2019/01/02 Python
使用TensorFlow实现二分类的方法示例
2019/02/05 Python
Django框架中间件(Middleware)用法实例分析
2019/05/24 Python
django foreignkey外键使用的例子 相当于left join
2019/08/06 Python
python3 map函数和filter函数详解
2019/08/26 Python
python使用matplotlib绘制折线图的示例代码
2020/09/22 Python
CSS3制作Dropdown下拉菜单的方法
2015/07/18 HTML / CSS
Gap工厂店:Gap Factory
2017/11/02 全球购物
李维斯牛仔裤英国官方网站:Levi’s英国
2019/10/10 全球购物
法律专业求职信
2014/05/24 职场文书
群众路线教育实践活动对照检查材料
2014/09/22 职场文书
大学学生会辞职信
2015/05/13 职场文书
乡镇团代会开幕词
2016/03/04 职场文书
详解Javascript实践中的命令模式
2021/05/05 Javascript
Hive导入csv文件示例
2022/06/25 数据库