index.php

core\xxt::run();//运行框架

core/xxt.php

<?php
namespace core;
use \core\lib\log;
use \core\lib\route;
class xxt
{
    public static $classMap = array();
    public $assign;
    static public function run()
    {
        log::init();
        $route = new route();//(1)new一个route对象,运行route构造方法
        $ctrlClass = $route->ctrl;
        $action = $route->action;
        $ctrlfile = APP.'/ctrl/'.$ctrlClass.'Ctrl.php';//(3)拼装控制器文件路径        
        $ctrlClass = '\\'.MODULE.'\ctrl\\'.$ctrlClass.'Ctrl';
        //(4)拼装控制器文件类名
        if(is_file($ctrlfile)) {
            include $ctrlfile;
            $ctrl = new $ctrlClass();
            if(method_exists ($ctrl, $action)) {
                $ctrl->$action();
                log::log('ctrl:'.$ctrlClass.'    '.'action:'.$action);
            }else{
                throw new \Exception('找不到方法'.$action);
            }
        }else{
            throw new \Exception('找不到控制器'.$ctrlClass);
        }
    }
}

core/lib/route.php

<?php
namespace core\lib;
class route
{
    public $ctrl;
    public $action;

    /**
     *生成对象属性
     * route constructor.
     */
    public function __construct()//(2)生成对象属性,对应控制器和方法
    {
        //xxx.com/index/index
        //xxx.com/index.php/index/index
        /**
         * 1、隐藏index.php .htaccess
         * 2、获取URL 参数部分
         * 3、返回对应的控制器和方法
         */
//        p($_SERVER);
        if(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != '/') {
            $path = $_SERVER['REQUEST_URI'];
            $patharr = explode('/',trim($path,'/'));
            if(isset($patharr[0])) {
                $this->ctrl = $patharr[0];
            }
            unset($patharr[0]);
            if(isset($patharr[1])) {
                $this->action = $patharr[1];
                unset($patharr[1]);
            }else{
                $this->action = conf::get('ACTION','route');
            }
            //url多余部分转换成 GET
            //id/1/str/2/test/3
            $count = count($patharr) + 2;
            $i = 2;
            while($i < $count) {
                if(isset($patharr[$i + 1])) {
                    $_GET[$patharr[$i]] = $patharr[$i+1];
                }
                $i = $i + 2;
            }

        }else{
            $this->ctrl = conf::get('CTRL','route');
            $this->action = conf::get('ACTION','route');
        }

    }
}
Scroll to Top