- 1. X-Router超級路由器(支持超多臺計算機同時上網的軟...
- 2. CrossLoop(實現異地屏幕共享的遠程協助工具) V2.80...
- 3. phpMyAdmin(支持對數據庫進行建立、復制,刪除數據等...
- 4. AirPort Utility for Mac (無線路由管理工具)V5.5....
- 5. AirPort Utility(蘋果無線路由管理工具)V5.5.3.2最...
- 6. QuickPHP V1.12.1(php腳本調試工具) 綠色免費版
- 7. RouterPassView(從路由器找回丟失密碼的文件 ) V1....
- 8. 菊子曰(實現離線發布博客文章的工具) V4.0 G25 簡體...
- 9. apwifi軟件無線路由器(提供高效安全的互聯網訪問) ...
- 10. phpMyAdmin(支持對數據庫進行完全操控) V3.4.2.0 F...
關于php url路由的實現
這篇文章提供分享給大家,是關于php url路由的實現,下面的詳細的解析,希望對各位有所幫助。
1.符合規則定義的偽靜態訪問路徑解析
對于"test.php/user/lists/normal/id/2.html" 可解析為
control = user,action = lists,filter = normal,order = id,curPage = 3
對于"test.php/users/lists.html" 可解析為
control = user,action = lists,filter = all,order = '',curPage = 1 可取得規則定義中的默認值
2.不符合規則定義的偽靜態路徑解析
action,control 不符合規則
對于"test.php/users/lists/all/id1/1.html" 報錯
試圖訪問不存在的頁面
不符合匹配模式
對于"test.php/user/lists/all/id1/1.html" 可解析為
control = user,action = lists,filter = all,order = '',curPage = 1
可取得不符合匹配模式項目的默認值,上例 order 不符合匹配模式
定義路由規則時可以定義默認值,當在pathinfo中找不到匹配的值,能取得默認值
<?php
// url 路由規則定義
$urlRule = array(
'user' => array( // control
'lists' => array( // action
//'名稱' => '默認值,值模式匹配'
'filter' => 'all,^(all|normal|admin)$',
'order' => ',^-?[a-zA-Z_]+$',
'curPage' => '1,^[0-9]+$',
),
),
);
function parseUrl(){
$queryString = array();
$GLOBALS['control'] = 'index';
$GLOBALS['action'] = 'index';
if (isset($_SERVER['PATH_INFO'])){
//獲取 pathinfo
$aPathInfo = explode('/', substr($_SERVER['PATH_INFO'], 1, strrpos($_SERVER['PATH_INFO'], '.')-1));
// 獲取 control
$GLOBALS['control'] = $aPathInfo[0];
array_shift($aPathInfo);
// 獲取 action
$GLOBALS['action'] = (isset($aPathInfo[0]) ? $aPathInfo[0] : 'index');
array_shift($aPathInfo);
// 獲取 入口文件名
$GLOBALS['PHP_SELF'] = str_replace($_SERVER['PATH_INFO'], '', $_SERVER['PHP_SELF']);
$queryString = $aPathInfo;
}
parseQueryString($queryString);
}
function parseQueryString(array$aQueryString){
$queryString = array();
// control 與 action 為默認值時
if ($GLOBALS['control'] == 'index' && $GLOBALS['action'] == 'index'){
$GLOBALS['queryString'] = $queryString;
return true;