一個(gè)簡(jiǎn)單的php路由類
更新時(shí)間:2016年05月29日 10:55:02 作者:imhuchao
這篇文章主要為大家詳細(xì)介紹了一個(gè)簡(jiǎn)單的php路由類,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了php編寫一個(gè)簡(jiǎn)單的路由類,供大家參考,具體內(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
希望本文所述對(duì)大家學(xué)習(xí)PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php實(shí)現(xiàn)計(jì)算百度地圖坐標(biāo)之間距離的方法
這篇文章主要介紹了php實(shí)現(xiàn)計(jì)算百度地圖坐標(biāo)之間距離的方法,涉及php字符串、數(shù)組及數(shù)學(xué)運(yùn)算的相關(guān)技巧,需要的朋友可以參考下2016-05-05
PHP單例模式數(shù)據(jù)庫連接類與頁面靜態(tài)化實(shí)現(xiàn)方法
這篇文章主要介紹了PHP單例模式數(shù)據(jù)庫連接類與頁面靜態(tài)化實(shí)現(xiàn)方法,涉及php面向?qū)ο髥卫J綌?shù)據(jù)庫連接類的定義與使用方法,以及緩存實(shí)現(xiàn)頁面靜態(tài)化相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
php+ajax實(shí)現(xiàn)仿百度查詢下拉內(nèi)容功能示例
這篇文章主要介紹了php+ajax實(shí)現(xiàn)仿百度查詢下拉內(nèi)容功能,結(jié)合具體實(shí)例形式分析了php結(jié)合ajax動(dòng)態(tài)查詢功能的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-10-10
php版阿里大于(阿里大魚)短信發(fā)送實(shí)例詳解
這篇文章主要介紹了php版阿里大于(阿里大魚)短信發(fā)送實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了阿里大于短信發(fā)送接口的配置與使用技巧,需要的朋友可以參考下2016-11-11
PHP中批量生成靜態(tài)html(命令行下運(yùn)行PHP)
這篇文章主要介紹了如何通過命令行下運(yùn)行PHP命令,減少web請(qǐng)求,讓網(wǎng)站運(yùn)行的更穩(wěn)定,生成速度也更快2014-04-04

