PHP實現(xiàn)一個簡單url路由功能實例
什么是php的路由機制
1、路由機制就是把某一個特定形式的URL結(jié)構(gòu)中提煉出來系統(tǒng)對應的參數(shù)。舉個例子,如:http://main.test.com/article/1 其中:/article/1 -> ?_m=article&id=1。
2、然后將擁有對應參數(shù)的URL轉(zhuǎn)換成特定形式的URL結(jié)構(gòu),是上面的過程的逆向過程。
如果一個頁面的內(nèi)容呈現(xiàn),需要根據(jù)url上傳遞的參數(shù)來進行渲染。很多時候可能是這樣子寫:xxx.com/xx?c=x&m=x&t=..,而我們看到的url往往是這樣子的(以新浪微游戲的咖啡戀人為例) game.weibo.com/ilovecoffee….這種URL設計看上去比前一種更好一點:)
如果我們訪問一下不存在的游戲應用,例如game.weibo.com/ilovecoffee222,則會輸出如下的錯誤提示:
game.weibo.com后面匹配到的項,指向了某個php頁面,然后根據(jù)參數(shù)獲取要訪問的游戲應用標識,后數(shù)據(jù)庫或者緩存里查詢該應用標識,如果不存在則輸出錯誤提示,如果應用存在則加載游戲應用鏈接地址。
現(xiàn)在寫一個php例子,假設我的ip為192.168.0.33,我加了一層名為router的路徑,之后跟隨的是 “/模塊名/方法名/參數(shù)1的key/參數(shù)1的value/….”
類似這樣的地址:
192.168.0.33/router/Hello/router/a/b/c/d/abc/index.html?id=3&url=http:………………
也就是要調(diào)用Ha這個模塊中的router方法,并傳入url后面的參數(shù)/a/b/c/d/index………….
第一步,首先要在服務器的配置上對/router/路徑進行攔截
調(diào)用某個文件夾目錄下的index.php頁面,假定現(xiàn)在所有模塊使用單獨的文件存放于class目錄下,該目錄與router平級,如下圖所示:

第二步,路由分發(fā)器的實現(xiàn)(index.php)
<!Doctype html>
<html>
<head>
<title>路由測試~~</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
date_default_timezone_set("Asia/Shanghai");
define("MODULE_DIR", "../class/");
$_DocumentPath = $_SERVER['DOCUMENT_ROOT'];
$_FilePath = __FILE__;
$_RequestUri = $_SERVER['REQUEST_URI'];
$_AppPath = str_replace($_DocumentPath, '', $_FilePath); //==>\router\index.php
$_UrlPath = $_RequestUri; //==>/router/hello/router/a/b/c/d/abc/index.html?id=3&url=http:
$_AppPathArr = explode(DIRECTORY_SEPARATOR, $_AppPath);
/**
* http://192.168.0.33/router/hello/router/a/b/c/d/abc/index.html?id=3&url=http:
*
* /hello/router/a/b/c/d/abc/index.html?id=3&url=http:
*/
for ($i = 0; $i < count($_AppPathArr); $i++) {
$p = $_AppPathArr[$i];
if ($p) {
$_UrlPath = preg_replace('/^\/'.$p.'\//', '/', $_UrlPath, 1);
}
}
$_UrlPath = preg_replace('/^\//', '', $_UrlPath, 1);
$_AppPathArr = explode("/", $_UrlPath);
$_AppPathArr_Count = count($_AppPathArr);
$arr_url = array(
'controller' => 'index',
'method' => 'index',
'parms' => array()
);
$arr_url['controller'] = $_AppPathArr[0];
$arr_url['method'] = $_AppPathArr[1];
if ($_AppPathArr_Count > 2 and $_AppPathArr_Count % 2 != 0) {
die('參數(shù)錯誤');
} else {
for ($i = 2; $i < $_AppPathArr_Count; $i += 2) {
$arr_temp_hash = array(strtolower($_AppPathArr[$i])=>$_AppPathArr[$i + 1]);
$arr_url['parms'] = array_merge($arr_url['parms'], $arr_temp_hash);
}
}
$module_name = $arr_url['controller'];
$module_file = MODULE_DIR.$module_name.'.class.php';
$method_name = $arr_url['method'];
if (file_exists($module_file)) {
include $module_file;
$obj_module = new $module_name();
if (!method_exists($obj_module, $method_name)) {
die("要調(diào)用的方法不存在");
} else {
if (is_callable(array($obj_module, $method_name))) {
$obj_module -> $method_name($module_name, $arr_url['parms']);
$obj_module -> printResult();
}
}
} else {
die("定義的模塊不存在");
}
?>
</body>
</html>
獲取請求的uri,然后拿到要加載的模塊名、調(diào)用方法名,對uri參數(shù)進行簡單的判斷..
第三步,模塊的編寫
根據(jù)上述的uri,我們要調(diào)用的是Hello模塊下的router方法,那么可以在class目錄下定義一個名為Hello.class.php的文件(注意linux下是區(qū)分大小寫的)
<?php
class Hello {
private $_name;
private $_varValue;
function __construct() {
}
function router() {
$this->_name = func_get_arg(0);
$this->_varValue = func_get_arg(1);
}
function printResult() {
echo $this->_name;
echo "<p>";
echo var_dump($this->_varValue);
echo "</p>";
}
}
?>
同理,我們可以編寫Ha模塊..
這算是實現(xiàn)了很簡單的url路由分發(fā)功能了…
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在Laravel 中實現(xiàn)是否關(guān)注的示例
今天小編就為大家分享一篇在Laravel 中實現(xiàn)是否關(guān)注的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
php while循環(huán)得到循環(huán)次數(shù)
在for循環(huán)中,我們很容易得到循環(huán)次數(shù),因為是作為條件出現(xiàn)的。在while也可以得到,如下:2013-10-10

