thinkphp路由規則使用樣本詳解和偽靜態功能實現(apache重寫)_PHP教程

來源:互聯網
上載者:User
複製代碼 代碼如下:
//thinkphp 路由定義規則
$route = array(
'news/:action/:year\d/:month/:day'=>'news/read?year=:2&month=:3&day=:4',
'news/:action^delete|update|insert/:year\d/:month/:day'=>array( 'news/read?extra=:2&status=1','year=:2&month=:3&day=:4'),
);

$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); //去除靜態匹配項
}


//擷取數組中的值,目的是配合子模式進行替換
$values = array_values($matches);
//正則匹配替換,正則需要用'e'作為修飾符
$url = preg_replace('/:(\d+)/e','$values[\\1-1]',$url);

//解析url 格式: 分組/模組/操作?key1=value1&key2=value2
if(strpos($url,'?')!==false){
// 分組/模組/操作?key1=value1&key2=value2
$arr = parse_url($url);
$paths = explode('/',$arr['path']);
parse_str($arr['query'],$queryArr);
}elseif(strpos($url,'/')!==false) //分組/模組/操作)
$paths = explode('/',$url);
else // key1=value1&key2=value2
parse_str($url,$queryArr);


//擷取 分組 模組 操作
if(!empty($paths)){
$var['actionName'] = array_pop($paths);
$var['moduleName'] = array_pop($paths);
if(!empty($paths)){
$groupList = 'Home,Admin';
$temp = array_pop($paths);
if(in_array($temp,explode(',',$groupList)))
$var['groupName'] = $temp;
}
}
//合并的到GET數組中,方便全域調用
$_GET = array_merge($_GET,$var);

//合并參數
if(isset($queryArr))
$_GET = array_merge($_GET,$queryArr);

//匹配url中剩餘的參數
preg_replace('/(\w+)\/([^,\/]+)/e','$tempArr[\'\\1\']=\'\\2\'',implode('/',$regxArr));
if(!empty($tempArr))
$_GET = array_merge($_GET,$tempArr);


//route是數組的話
if(is_array($route)){
$route[1]=preg_replace('/:(\d+)/e','$values[\\1-1]',$route[1]);
parse_str($route[1],$var);
$_GET = array_merge($_GET,$var);
strpos($url,'?')!==false ? $der ='&' : $der='?';
//最終寫入到$_GET中的參數,包括三個部分
//1.地址欄剩餘參數
//2.路由地址中的參數
//3.$route是數組時的第二個參數
if(!empty($tempArr))
$var = array_merge($tempArr,$var);
$url .=$der.http_build_query($var);
}
$_GET['finalUrl'] = $url;
//保證$_REQUEST 也能訪問
$_REQUEST = array_merge($_REQUEST,$_GET);
//結果
print_r($_GET);
return true;
}
return $match;
}


//以下是正則路由代碼:
$rule = '/news\/read\/(\d+)\/(\d+)\/(\d+)/';
$route ='news/read?year=:1&month=:2&day=:3';
$regx = 'news/read/2012/2/21/extraparam/test.html';
$extension = 'html';
parseUrlRuleRegx($rule,$route,$regx,$extension);


/**
* @$rule string 路由規則
* @$route string 規則映射的新地址
* @$regx string 地址欄pathinfo字串
* @$extension stirng 偽靜態拓展名
* return bool
*/
function parseUrlRuleRegx($rule,$route,$regx,$extension=null){
!is_null($extension) && $regx = str_replace('.'.$extension,'',$regx);
$url = is_array($route) ? $route[0] : $route;
if(preg_match($rule,$regx,$matches)){
$url = preg_replace('/:(\d+)/e','$matches[\\1]',$url);
}else
return false;
//解析url 格式: 分組/模組/操作?key1=value1&key2=value2
if(strpos($url,'?')!==false){
// 分組/模組/操作?key1=value1&key2=value2
$arr = parse_url($url);
$paths = explode('/',$arr['path']);
parse_str($arr['query'],$queryArr);
}elseif(strpos($url,'/')!==false) //分組/模組/操作)
$paths = explode('/',$url);
else // key1=value1&key2=value2
parse_str($url,$queryArr);


//擷取 分組 模組 操作
if(!empty($paths)){
$var['actionName'] = array_pop($paths);
$var['moduleName'] = array_pop($paths);
if(!empty($paths)){
$groupList = 'Home,Admin';
$temp = array_pop($paths);
if(in_array($temp,explode(',',$groupList)))
$var['groupName'] = $temp;
}
}
//合并的到GET數組中,方便全域調用
$_GET = array_merge($_GET,$var);
if(isset($queryArr))
$_GET = array_merge($_GET,$queryArr);

//匹配剩餘的參數
$regx = str_replace($matches[0],'',$regx);
preg_replace('/(\w+)\/([^,\/]+)/e','$tempArr[\'\\1\']=\'\\2\'',$regx);
if(!empty($tempArr)){
$_GET = array_merge($_GET,$tempArr);
strpos($url,'?')!==false ? $der='&':$der='?';
$url .=$der.http_build_query($tempArr);
}
if(is_array($route)){
$route[1] = preg_replace('/:(\d+)/e','$matches[\\1]',$route[1]);
parse_str($route[1],$var);
if(!empty($var)){
!empty($queryArr) && $var =array_merge($queryArr,$var);
$_GET= array_merge($_GET,$var);
}
strpos($url,'?')!==false ? $der='&':$der='?';
$url .=$der.http_build_query($var);
}

$_GET['finalUrl'] = $url;
print_r($_GET);
$_REQUEST = array_merge($_GET,$_REQUEST);
return true;
}

//運行結果:
//Array
// (
// [actionName] => read
// [moduleName] => news
// [year] => 2012
// [month] => 2
// [day] => 21
// [extraparam] => test
// [finalUrl] => news/read?year=2012&month=2&day=21&extraparam=test
// )
// [Finished in 0.1s]

http://www.bkjia.com/PHPjc/825265.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/825265.htmlTechArticle複製代碼 代碼如下: ?php //thinkphp 路由定義規則 $route = array( 'news/:action/:year\d/:month/:day'='news/read?year=:2day=:4', 'news/:action^delete|update|insert/:year...

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.