一個(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)文章
jQuery+php簡(jiǎn)單實(shí)現(xiàn)全選刪除的方法
這篇文章主要介紹了jQuery+php簡(jiǎn)單實(shí)現(xiàn)全選刪除的方法,涉及php結(jié)合jQuery操作表單的全選及ajax交互實(shí)現(xiàn)刪除的相關(guān)技巧,需要的朋友可以參考下2016-11-11
PHP學(xué)習(xí)筆記 用戶注冊(cè)模塊用戶類以及驗(yàn)證碼類
最近正在學(xué)習(xí)《PHP&MySQL范例精解》,剛剛看到第一張,關(guān)于用戶注冊(cè)模塊的設(shè)計(jì),這本書提供了很多可重用類,便于用于其它項(xiàng)目中。2011-09-09
PHP高效獲取遠(yuǎn)程圖片尺寸和大小的實(shí)現(xiàn)方法
這篇文章主要介紹了 PHP高效獲取遠(yuǎn)程圖片尺寸和大小的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文大家能夠?qū)崿F(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10
php+mysql查詢實(shí)現(xiàn)無(wú)限下級(jí)分類樹輸出示例
這篇文章主要介紹了php+mysql查詢實(shí)現(xiàn)無(wú)限下級(jí)分類樹輸出,結(jié)合實(shí)例形式分析了php+MySQL查詢實(shí)現(xiàn)的樹狀分類輸出功能,涉及php數(shù)據(jù)庫(kù)查詢與數(shù)組遍歷等相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
Php中用PDO查詢Mysql來(lái)避免SQL注入風(fēng)險(xiǎn)的方法
本篇文章介紹了,Php中用PDO查詢Mysql來(lái)避免SQL注入風(fēng)險(xiǎn)的方法。需要的朋友參考下2013-04-04

