這篇文章主要介紹了thinkphp路由規則使用示例詳解和偽靜態功能實現(apache重寫),需要的朋友可以參考下
$url = 'http://www.test.com/index.php/news/read/2012/2/21/extraparam/test.html';
//后綴名
$extension = 'html';
//可知: $_SERVER['PATH_INFO'] = 'news/read/2012/2/21/extraparam/test.html';
$regx = 'news/read/2012/2/21/extraparam/test.html';
//循環匹配路由規則
foreach($route as $key=>$value){
//如果匹配成功,則不繼續匹配
if(parseUrlRule($key,$value,$regx,$extension))
break;
}
//運行結果: 打印$_GET
//Array
// (
// [actionName] => read
// [moduleName] => news
// [extra] => 2012
// [status] => 1
// [extraparam] => test
// [year] => 2012
// [month] => 2
// [day] => 21
// [finalUrl] => news/read?extra=2012&status=1&extraparam=test&year=2012&month=2&day=21
// )
// [Finished in 0.6s]
//相當于訪問: http://www.test.com/news/read?extra=2012&status=1&extraparam=test&year=2012&month=2&day=21
//在部署時會把index.php隱藏,開啟apache的重寫模塊
//重寫規則 : RewriteRule ^(.+)$ /index.php/$1
//開啟后,apache會自動把 http:/www.test.com/news/read/2012/2/21/extraparam/test.html轉換為 http:/www.test.com/index.php/news/read/2012/2/21/extraparam/test.html
/**
* @$rule string 路由規則
* @$route string 規則映射的新地址
* @$regx string 地址欄pathinfo字符串
* @$extension stirng 偽靜態拓展名
* return bool
*/
function parseUrlRule($rule,$route,$regx,$extension=null){
//去掉后綴名
!is_null($extension) && $regx = str_replace('.'.$extension,'',$regx);
//把路由規則和地址,分割到數組中,然后逐項匹配
$ruleArr = explode('/',$rule);
$regxArr = explode('/',$regx);
//$route以數組的格式傳遞,則取第一個
$url = is_array($route) ? $route[0] : $route;
$match =true;
//匹配檢測
foreach($ruleArr as $key=>$value){
if(strpos($value,':')===0){
if(substr($value,-2)=='//d' && !is_numeric($regxArr[$key])){
$match = false;
break;
}elseif(strpos($value,'^')){
$stripArr = explode('|',trim(strstr($value,'^'),'^'));
if(in_array($regxArr[$key],$stripArr)){
$match = false;
break;
}
}
//靜態項不區分大小寫
}elseif(strcasecmp($value, $regxArr[$key])!==0) {
$match = false;
break;
}
}
//匹配成功
if($match){
//把動態變量寫入到數組$matches 中,同時去除靜態匹配項
foreach($ruleArr as $key=>$value){
if(strpos($value,':')===0){
//獲取動態變量,作為數組下標
if(substr($value,-2,1)=='//')
$matchKey = substr($value,1,-2);
elseif($pos=strpos($value,'^'))
$matchKey =substr($value,1,$pos-1);
else
$matchKey = substr($value,1);
$matches[$matchKey] = array_shift($regxArr);
}else
array_shift($regxArr); //去除靜態匹配項
}
//獲取數組中的值,目的是配合子模式進行替換
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb