PHP头条
热点:

自己所理解的php路由-PHP源码


1. [代码]用到的信息

 false,
    //路由规则
    'ROUTE' => array(
        //[0],[1]匹配后面的参数0,参数1 \d,\d依次表示[0],[1]的类型 正则
        //'test-[0]-[1].html' => array('控制器', '动作', array('参数0', '参数2'), array('\d', '\d+'))
    )
);

2. [PHP]代码

$Action();
                }else{
                    self::Show('控制器动作不存在!');
                }
            }else{
                self::Show('控制器无法加载!');
            }
        }else{
            self::Show('控制器['. $ControlPath .']不存在!');
        }
    }

    /**
     * 编译路由
     * @param string $Controller 控制器
     * @param string $Action     动作
     * @param string $Param      参数
     * @return bool|mixed|string
     */
    public static function Route($Controller, $Action, $Param){
        $routes = C('Epoch.ROUTE');
        if(is_array($routes)){
            foreach($routes as $pattern => $route){
                $uri = false;
                //必须有控制器名和动作名
                if(empty($route[0]) || empty($route[1])){
                    continue;
                }
                //获取参数是否匹配
                preg_match_all('/[\d+]/', $pattern, $match);
                if(count($match[0]) != count($route[2])){
                    continue;
                }
                //检查控制器和动作是否相同
                if($route[0] == $Controller && $route[1] == $Action){
                    $parse = parse_url_param($Param);
                    $flag  = 1;
                    foreach($route[2] as $key => $param){
                        if(! isset($parse[$param])){
                            $flag = 0;
                            $uri  = false;
                            break;
                        }
                        //替换参数
                        $uri = str_replace('['. $key .']', $parse[$param], $pattern);
                        unset($parse[$param]);
                    }
                    //如果匹配失败 则跳过
                    if($flag == 0){
                        continue;
                    }
                    //检查是否还有剩余的参数
                    if(!empty($parse)) {
                        $extParam = '?';
                        foreach ($parse as $key => $value){
                            $extParam .= $key .'='. $value .'&';
                        }
                        $extParam = substr($extParam, 0, -1);
                    }
                    //组装剩余的参数
                    $uri .= $extParam;
                    if($uri !== false){
                        return $uri;
                    }
                }
            }
        }
        return false;
    }
    /**
     * 解析路由
     */
    protected static function ParseRoute(){
        $routes = C('Epoch.ROUTE');
        $pathinfo = $_GET['pathinfo'];
        if(! is_array($routes)){
            return;
        }
        foreach($routes as $pattern => $route){
            //组装正则表达式
            for($i = 0; $i < 10; $i++){
                $res      = isset($route[3][$i]) ? $route[3][$i] : '[A-Za-z0-9]';
                $pattern  = str_replace('['. $i .']', '('. $res .'+)', $pattern);
            }
            $pattern  = '/^'. $pattern .'$/';
            //匹配路由
            if(preg_match($pattern, $pathinfo, $match)){
                unset($match[0]);
                //写入数据
                $_GET[C('Epoch.VAR_CONTROLLER')] = $route[0];
                $_GET[C('Epoch.VAR_ACTION')]     = $route[1];
                foreach($match as $key => $value){
                    $_GET[$route[2][$key - 1]] = $value;
                }
            }
        }
    }


    public static function Show($msg){
        EpochException::Show($msg);
    }
}

www.phpzy.comtrue/php/39924.htmlTechArticle自己所理解的php路由-PHP源码 1. [代码]用到的信息 false, //路由规则 ROUTE => array( //[0],[1]匹配后面的参数0,参数1 \d,\d依次表示[0],[1]的类型 正则 //test-[0]-[1].html => array(控制器, 动作, array(参数...

相关文章

PHP之友评论

今天推荐