Heim  >  Artikel  >  php教程  >  自己所理解的php路由

自己所理解的php路由

PHP中文网
PHP中文网Original
2016-05-23 08:39:14983Durchsuche

1. [代码]用到的信息   

<?php
array(
    //启用路由 默认不启用
    &#39;ROUTE_ON&#39; => false,
    //路由规则
    &#39;ROUTE&#39; => array(
        //[0],[1]匹配后面的参数0,参数1 \d,\d依次表示[0],[1]的类型 正则
        //&#39;test-[0]-[1].html&#39; => array(&#39;控制器&#39;, &#39;动作&#39;, array(&#39;参数0&#39;, &#39;参数2&#39;), array(&#39;\d&#39;, &#39;\d+&#39;))
    )
);

2. [PHP]代码   

<?php
/**
 * Url路由
 * @author 布衣
 * QQ 780998900
 */
class Dispatcher{

    public static function start(){
        //检测pathinfo参数存在且启用路由则解析路由
        if($_GET[&#39;pathinfo&#39;] && C(&#39;Epoch.ROUTE_ON&#39;) == true){
            self::ParseRoute();
        }
        //获取控制器
        $Control = $_GET[C(&#39;Epoch.VAR_CONTROLLER&#39;)];
        //获取动作
        $Action  = $_GET[C(&#39;Epoch.VAR_ACTION&#39;)];

        //检查子域名部署模式
        //$doamin = C(&#39;Epoch.DOMAIN&#39;);
        //if(count($doamin)){

        //}

        $Control = empty($Control) ? C(&#39;Epoch.DEFAULT_CONTROLLER&#39;) : $Control;
        $Action  = empty($Action)  ? C(&#39;Epoch.DEFAULT_ACTION&#39;)     : $Action;
        $ControlPath = APP_PATH .&#39;Controller/&#39;. $Control . C(&#39;Epoch.DEFAULT_CONTROLLER_SUFFIX&#39;);
        if(file_exists($ControlPath)){
            require_once $ControlPath;
            $Controlor = $Control .&#39;Controller&#39;;
            if(class_exists($Controlor)){
                $obj = new $Controlor();
                if(method_exists($obj, $Action)){
                    $obj->$Action();
                }else{
                    self::Show(&#39;控制器动作不存在!&#39;);
                }
            }else{
                self::Show(&#39;控制器无法加载!&#39;);
            }
        }else{
            self::Show(&#39;控制器[&#39;. $ControlPath .&#39;]不存在!&#39;);
        }
    }

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


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

                   

                   

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn