thinkphp 3.2.3 - Route.class.php 解析(路由匹配)

 

class Route {
    public static function check(){
        $depr   =   C('URL_PATHINFO_DEPR'); // '/'
        $regx   =   preg_replace('/\.'.__EXT__.'$/i','',trim($_SERVER['PATH_INFO'],$depr));
        // 分隔符替换 确保路由定义使用统一的分隔符
        if('/' != $depr){
            $regx = str_replace($depr,'/',$regx);
        }
        
        
        
        // URL映射定义(静态路由)
        // http://www.domain.com/?s=pathinfo1
        // http://www.domain.com/pathinfo1
        $maps   =   C('URL_MAP_RULES');
//         {
//             $maps = array(
//                 "pathinfo1"=>'module1/ctrl1/action1',
//                 "pathinfo2"=>'module2/ctrl2/action2'
//             );
//         }
        if(isset($maps[$regx])) {
            $var    =   self::parseUrl($maps[$regx]);
//             {
//                 $var[C('VAR_ACTION')] = array_pop($path);
//                 if(!empty($path)) {
//                     $var[C('VAR_CONTROLLER')] = array_pop($path);
//                 }
//                 if(!empty($path)) {
//                     $var[C('VAR_MODULE')]  = array_pop($path);
//                 }
//             }
            $_GET   =   array_merge($var, $_GET);
            return true;
        }
        
        // 动态路由处理
        $routes =   C('URL_ROUTE_RULES');
        if(!empty($routes)) {
//             $routes = array(
//                 // 写法
// //                 'rule1'=>array(0=>'',1=>'',2=>array('ext'=>'.html','method'=>'GET','callback'=>'func1')),
// //                 array(0=>'rule1',1=>'',2=>array('ext'=>'.html','method'=>'GET','callback'=>'func1')),
// //                 array(0=>'rule2',1=>'',2=>array('ext'=>'.html','method'=>'POST','callback'=>function(){ return true;/* 匹配成功 return true; */ return false; })),
                
//                 // 正则路由
//                 '/[a-z]\/[a-z]\/[a-z]/' => function($module,$ctrl,$action){  return true;/* 匹配成功 return true; */ return false; },
//                 '/[a-z]\/[a-z]\/[a-z]/'=>array(0=>'',1=>'',2=>array('ext'=>'.html','method'=>'GET','callback'=>function(){ return true;/* 匹配成功 return true; */ return false; })),
//                 // 内部重写,/ctrl1/xxx/zzz                          --->  Ctrl1/action1?id=xxx&page=zzz&cate=1            | $_GET = array('c'=>'Ctrl1','a'=>'action1','id'=>'xxx','page'=>'zzz','cate'=>'1','static_param1'=>'static_param1_valu1');
//                 '/ctrl1\/[a-z]\/[a-z]/'=>array(0=>'Ctrl1/action1?id=:1&page=:2&cate=1',1=>array('static_param1'=>'static_param1_valu1'),2=>array('ext'=>'.html','method'=>'GET','callback'=>function(){ return true;/* 匹配成功 return true; */ return false; })),
//                 // 内部重写,/ctrl1/xxx/zzz                             --->  Module1/Ctrl1/action1?id=xxx&page=zzz&cate=1 | $_GET = array('m'=>'Module1','c'=>'Ctrl1','a'=>'action1','id'=>trim('xxx'),'page'=>'zzz','cate'=>'1','static_param1'=>'static_param1_valu1');
//                 '/ctrl1\/[a-z]\/[a-z]/'=>array(0=>'Module1/Ctrl1/action1?id=:1|trim&page=:2&cate=1',1=>array('static_param1'=>'static_param1_valu1'),2=>array('ext'=>'.html','method'=>'GET','callback'=>function(){ return true;/* 匹配成功 return true; */ return false; })),
//                 // 内部重写,/ctrl1/xxx/zzz/param1/param1_value      --->  Module1/Ctrl1/action1?id=xxx&page=zzz&cate=1 | $_GET = array('m'=>'Module1','c'=>'Ctrl1','a'=>'action1','id'=>trim('xxx'),'page'=>'zzz','cate'=>'1','param1'=>'param1_value','static_param1'=>'static_param1_valu1');
//                 '/ctrl1\/[a-z]\/[a-z]/'=>array(0=>'Module1/Ctrl1/action1?id=:1|trim&page=:2&cate=1',1=>array('static_param1'=>'static_param1_valu1'),2=>array('ext'=>'.html','method'=>'GET','callback'=>function(){ return true;/* 匹配成功 return true; */ return false; })),
//                 // 重定向
//                 '/tobaidu'=>array(0=>'http://www.baidu.com',1=>'301',2=>array('ext'=>'.html','method'=>'GET','callback'=>function(){ return true;/* 匹配成功 return true; */ return false; })),
//                 '/tobaidu'=>array(0=>'http://www.baidu.com',1=>'302',2=>array('ext'=>'.html','method'=>'GET','callback'=>function(){ return true;/* 匹配成功 return true; */ return false; })),
//                 '/tourl'=>array(0=>'/desurl',1=>'301',2=>array('ext'=>'.html','method'=>'GET','callback'=>function(){ return true;/* 匹配成功 return true; */ return false; })),
                
//                 // 规则路由
//                 '[:param1|trim]'=>function($param1){  return true;/* 匹配成功 return true; */ return false; },
//                 '[:param1]/[:param2]'=>function($param1,$param2){  return true;/* 匹配成功 return true; */ return false; },
//                  // 内部重写,/10086/param1/param1_value     --->  Module1/Ctrl1/action1?id=10086&cate=1 | $_GET = array('m'=>'Module1','c'=>'Ctrl1','a'=>'action1','id'=>trim('10086'),'cate'=>'1','param1'=>'param1_value','static_param1'=>'static_param1_valu1');
//                 '[:param1\d|trim]'=>array(0=>'Module1/Ctrl1/action1?id=:1&cate=1',1=>array('static_param1'=>'static_param1_valu1'),2=>array('ext'=>'.html','method'=>'GET','callback'=>function(){ return true;/* 匹配成功 return true; */ return false; })),
//                  // 内部重写,/xxx/param1/param1_value      --->  Module1/Ctrl1/action1?id=xxx&cate=1 | $_GET = array('m'=>'Module1','c'=>'Ctrl1','a'=>'action1','id'=>trim('xxx'),'cate'=>'1','param1'=>'param1_value','static_param1'=>'static_param1_valu1');
//                 '[:param1^xxx-ctrl2-ctrl3|trim]'=>array(0=>'Module1/Ctrl1/action1?id=:1&cate=1',1=>array('static_param1'=>'static_param1_valu1'),2=>array('ext'=>'.html','method'=>'GET','callback'=>function(){ return true;/* 匹配成功 return true; */ return false; })),
//                  // 内部重写,/xxx/zzz/param1/param1_value      --->  Module1/Ctrl1/action1?id=xxx&page=zzz&cate=1 | $_GET = array('m'=>'Module1','c'=>'Ctrl1','a'=>'action1','id'=>trim('xxx'),'page'=>trim('zzz'),'cate'=>'1','param1'=>'param1_value','static_param1'=>'static_param1_valu1');
//                 '[:param1^xxx-module2-module3]/[:param1^zzz-ctrl2-ctrl3]/[:param1^action1-action2-action3]'=>array(0=>'Module1/Ctrl1/action1?id=:1&page=:2&cate=1',1=>array('static_param1'=>'static_param1_valu1'),2=>array('ext'=>'.html','method'=>'GET','callback'=>function(){ return true;/* 匹配成功 return true; */ return false; })),
//                  // 完整匹配,/xxx/zzz      --->  Module1/Ctrl1/action1?id=xxx&page=zzz&cate=1 | $_GET = array('m'=>'Module1','c'=>'Ctrl1','a'=>'action1','id'=>trim('xxx'),'page'=>trim('zzz'),'cate'=>'1');
//                 'xxx/zzz$'=>array(0=>'Module1/Ctrl1/action1?id=:1&page=:2&cate=1',1=>array('static_param1'=>'static_param1_valu1'),2=>array('ext'=>'.html','method'=>'GET','callback'=>function(){ return true;/* 匹配成功 return true; */ return false; })),
//                  // 重定向, /xxx/param1/param1_value      --->  /desurl
//                 '[:param1^xxx-module2-module3]'=>array(0=>'/desurl',1=>'301',2=>array('ext'=>'.html','method'=>'GET','callback'=>function(){ return true;/* 匹配成功 return true; */ return false; })),
//             );
            foreach ($routes as $rule=>$route){
                if(is_numeric($rule)){
                    // 支持 array('rule','adddress',...) 定义路由
                    $rule   =   array_shift($route);
                }
                if(is_array($route) && isset($route[2])){

                    // 路由参数
                    $options    =   $route[2];
                    if(isset($options['ext']) && __EXT__ != $options['ext']){
                        // URL后缀检测
                        continue;
                    }
                    if(isset($options['method']) && REQUEST_METHOD != strtoupper($options['method'])){
                        // 请求类型检测
                        continue;
                    }
                    // 自定义检测
                    if(!empty($options['callback']) && is_callable($options['callback'])) {
                        if(false === call_user_func($options['callback'])) {
                            continue;
                        }
                    }
                }
                if(0===strpos($rule,'/') && preg_match($rule,$regx,$matches)) { // 正则路由
                    if($route instanceof \Closure) {
                        // 执行闭包
                        $result = self::invokeRegx($route, $matches);
                        // 如果返回布尔值 则继续执行
                        return is_bool($result) ? $result : exit;
                    }else{
                        
                        return self::parseRegex($matches,$route,$regx);
                    }
                }else{ // 规则路由
                    $len1   =   substr_count($regx,'/'); 
                    $len2   =   substr_count($rule,'/');
                    if($len1>=$len2 || strpos($rule,'[')) { // “访问路径的长度”大于“规则的长度”
                        if('$' == substr($rule,-1,1)) {// 完整匹配
                            if($len1 != $len2) {
                                continue;
                            }else{
                                $rule =  substr($rule,0,-1);
                            }
                        }
                        $match  =  self::checkUrlMatch($regx,$rule);
                        if(false !== $match)  {
                            if($route instanceof \Closure) {
                                // 执行闭包
                                $result = self::invokeRule($route, $match);
                                // 如果返回布尔值 则继续执行
                                return is_bool($result) ? $result : exit;
                            }else{
                                return self::parseRule($rule,$route,$regx);
                            }
                        }
                    }
                }
            }
        }
        return false;
    }
    
    /**
     * “规则路由”检测器
     * 
     * 检测URL和规则路由是否匹配
     */
    private static function checkUrlMatch($regx,$rule) {
        $m1 = explode('/',$regx); // $_SERVER['PATH_INFO']
        $m2 = explode('/',$rule); // module1/ctrl1/act1/[:param1\d]
        $var = array();
        foreach ($m2 as $key=>$val){
            if(0 === strpos($val,'[:')){ // 如: $val == "[:param1\d]"
                $val    =   substr($val,1,-1);
            }
    
            if(':' == substr($val,0,1)) {// 动态变量   如: $val == ":param1\d"
                if($pos = strpos($val,'|')){
                    // 使用函数过滤
                    $val   =   substr($val,1,$pos-1);
                }
                if(strpos($val,'\\')) {  // 如: $val == ":param1\d"
                    $type = substr($val,-1); // 最后一个字符
                    if('d'==$type) {
                        if(isset($m1[$key]) && !is_numeric($m1[$key]))
                            return false;
                    }
                    $name = substr($val, 1, -2); // 变量名称
                }elseif($pos = strpos($val,'^')){ // 如: $val == ":param1^ctrl1-ctrl2-ctrl3"
                    $array   =  explode('-',substr(strstr($val,'^'),1));
                    if(in_array($m1[$key],$array)) {
                        return false;
                    }
                    $name = substr($val, 1, $pos - 1);
                }else{ // 如: $val == "param1"
                    $name = substr($val, 1);
                }
                $var[$name] = isset($m1[$key])?$m1[$key]:'';
            }elseif(0 !== strcasecmp($val,$m1[$key])){
                return false;
            }
        }
        // 成功匹配后返回URL中的动态变量数组
        return $var;
    }
    
    // 解析规则路由
    // '路由规则'=>'[控制器/操作]?额外参数1=值1&额外参数2=值2...'
    // '路由规则'=>array('[控制器/操作]','额外参数1=值1&额外参数2=值2...')
    // '路由规则'=>'外部地址'
    // '路由规则'=>array('外部地址','重定向代码')
    // 路由规则中 :开头 表示动态变量
    // 外部地址中可以用动态变量 采用 :1 :2 的方式
    // 'news/:month/:day/:id'=>array('News/read?cate=1','status=1'),
    // 'new/:id'=>array('/new.php?id=:1',301), 重定向
    private static function parseRule($rule,$route,$regx) {
        // 获取路由地址规则
        $url   =  is_array($route)?$route[0]:$route; // 路由目标的配置
        // 获取URL地址中的参数
        $paths = explode('/',$regx); // 访问地址
        // 解析路由规则
        $matches  =  array();
        $rule =  explode('/',$rule); // 匹配规则
        foreach ($rule as $item){
            $fun    =   '';
            if(0 === strpos($item,'[:')){
                $item   =   substr($item,1,-1);
            }
            if(0===strpos($item,':')) { // 动态变量获取
                if($pos = strpos($item,'|')){
                    // 支持函数过滤
                    $fun  =  substr($item,$pos+1);
                    $item =  substr($item,0,$pos);
                }
                if($pos = strpos($item,'^') ) {
                    $var  =  substr($item,1,$pos-1);
                }elseif(strpos($item,'\\')){
                    $var  =  substr($item,1,-2);
                }else{
                    $var  =  substr($item,1);
                }
                $matches[$var] = !empty($fun)? $fun(array_shift($paths)) : array_shift($paths);
            }else{ // 过滤URL中的静态变量
                array_shift($paths);
            }
        }
    
        if(0=== strpos($url,'/') || 0===strpos($url,'http')) { // 路由重定向跳转
            if(strpos($url,':')) { // 传递动态参数
                $values = array_values($matches);
                $url = preg_replace_callback('/:(\d+)/', function($match) use($values){ return $values[$match[1] - 1]; }, $url);
            }
            header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301);
            exit;
        }else{
            // 解析路由地址
            $var  =  self::parseUrl($url);
            // 解析路由地址里面的动态参数
            $values  =  array_values($matches);
            foreach ($var as $key=>$val){
                if(0===strpos($val,':')) {
                    $var[$key] =  $values[substr($val,1)-1];
                }
            }
            $var   =   array_merge($matches,$var);
            // 解析剩余的URL参数
            if(!empty($paths)) {
                preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){ $var[strtolower($match[1])]=strip_tags($match[2]);}, implode('/',$paths));
            }
            // 解析路由自动传入参数
            if(is_array($route) && isset($route[1])) {
                if(is_array($route[1])){
                    $params     =   $route[1];
                }else{
                    parse_str($route[1],$params);
                }
                $var   =   array_merge($var,$params);
            }
            $_GET   =  array_merge($var,$_GET);
        }
        return true;
    }
}

 

转载于:https://www.cnblogs.com/xiaoyaogege/p/6269200.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值