PHP如何?簡單路由

來源:互聯網
上載者:User
PHP如何?簡單路由?本文主要為大家詳細介紹了一個簡單的php路由類,感興趣的小夥伴們可以參考一下。希望對大家有所協助。

本文執行個體為大家分享了php編寫一個簡單的路由類,供大家參考,具體內容如下

<?phpnamespace 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)  {    //處理Regex    $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;  } }

相關推薦:

thinkphp 路由規則終極詳解(附偽靜態)-菜鳥必看

Yii運行機制及路由詳解

PHP學習MVC架構之路由

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.