国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > Apache > 正文

thinkphp路由規(guī)則使用示例詳解和偽靜態(tài)功能實現(xiàn)(apache重寫)

2024-08-27 18:25:37
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:
<?php
//thinkphp 路由定義規(guī)則  
$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';

//循環(huán)匹配路由規(guī)則
foreach($route as $key=>$value){
  //如果匹配成功,則不繼續(xù)匹配
  if(parseUrlRule($key,$value,$regx,$extension))
   break;
}

//運行結(jié)果: 打印$_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的重寫模塊
//重寫規(guī)則 : RewriteRule  ^(.+)$  /index.php/$1
//開啟后,apache會自動把 http:/www.test.com/news/read/2012/2/21/extraparam/test.html轉(zhuǎn)換為 http:/www.test.com/index.php/news/read/2012/2/21/extraparam/test.html

/**
 *  @$rule  string    路由規(guī)則  
 *  @$route string    規(guī)則映射的新地址
 *  @$regx  string    地址欄pathinfo字符串
 *  @$extension stirng  偽靜態(tài)拓展名
 *  return  bool
 */
function parseUrlRule($rule,$route,$regx,$extension=null){
   //去掉后綴名
   !is_null($extension) && $regx = str_replace('.'.$extension,'',$regx);

   //把路由規(guī)則和地址,分割到數(shù)組中,然后逐項匹配
   $ruleArr = explode('/',$rule);
   $regxArr = explode('/',$regx);

   //$route以數(shù)組的格式傳遞,則取第一個
   $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;

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 军事| 广丰县| 通化县| 怀化市| 和平区| 晴隆县| 芮城县| 沂南县| 神木县| 化隆| 观塘区| 元朗区| 长阳| 景洪市| 武威市| 图片| 林芝县| 宁津县| 昌吉市| 泗水县| 会理县| 寿宁县| 务川| 磴口县| 永修县| 富顺县| 米脂县| 广州市| 莆田市| 高邮市| 永安市| 昭苏县| 海宁市| 晴隆县| 积石山| 汪清县| 连城县| 昌图县| 兴和县| 积石山| 广汉市|