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

一個簡單的php路由類

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

本文實(shí)例為大家分享了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)
  {
    //處理正則表達(dá)式
    $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

希望本文所述對大家學(xué)習(xí)PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論

沐川县| 隆昌县| 蓬溪县| 平昌县| 汝南县| 黄山市| 武安市| 平泉县| 日照市| 甘孜| 濉溪县| 中牟县| 广安市| 军事| 崇仁县| 永仁县| 思茅市| 高陵县| 丰台区| 黄浦区| 石河子市| 读书| 土默特右旗| 萝北县| 通道| 河津市| 南投市| 萨嘎县| 绥阳县| 杂多县| 沁阳市| 金秀| 改则县| 永州市| 衡水市| 静乐县| 汉沽区| 察雅县| 朝阳市| 革吉县| 濉溪县|