一个简单的php路由类
来源: 阅读:724 次 日期:2016-08-18 15:19:49
温馨提示: 小编为您整理了“一个简单的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路由类
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们| 联系我们| 人才招聘| 网站声明| 网站帮助| 非正式的简要咨询| 简要咨询须知| 加入群交流| 手机站点| 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:
咨询QQ:526150442(9:00—18:00) 版权所有:
云南网警报警专用图标
Baidu
map