PHP如何實作簡單路由?本文主要為大家詳細介紹了一個簡單的php路由類,有興趣的朋友可以參考一下。希望對大家有幫助。
本文實例為大家分享了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; } }
相關推薦:
以上是PHP如何實作簡單路由的詳細內容。更多資訊請關注PHP中文網其他相關文章!