最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一個簡單的php路由類

 更新時間:2016年05月29日 10:55:02   作者:imhuchao  
這篇文章主要為大家詳細介紹了一個簡單的php路由類,感興趣的小伙伴們可以參考一下

本文實例為大家分享了php編寫一個簡單的路由類,供大家參考,具體內(nèi)容如下

<?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("http://", "/", 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程序設計有所幫助。

相關(guān)文章

最新評論

玉山县| 龙岩市| 黑龙江省| 和龙市| 永登县| 兴业县| 蒙城县| 宁化县| 广东省| 那坡县| 瑞金市| 全南县| 沙雅县| 广水市| 远安县| 桑植县| 宁都县| 西青区| 岚皋县| 喜德县| 武陟县| 鞍山市| 衡阳县| 舒城县| 隆回县| 阜康市| 邵阳县| 成都市| 香港 | 额济纳旗| 泰州市| 秭归县| 南陵县| 吴忠市| 武清区| 芷江| 张掖市| 永康市| 嘉祥县| 资阳市| 育儿|