当前位置: 移动技术网 > IT编程>开发语言>PHP > thinkphp路由规则使用示例详解和伪静态功能实现(apache重写)

thinkphp路由规则使用示例详解和伪静态功能实现(apache重写)

2019年03月30日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:<?php //thinkphp 路由定义规则  $route = array(  'news/:acti

复制代码 代码如下:

<?php
//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]

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网