搭建一个简单的mvc(一)

来源:互联网 发布:ubuntu 进入boot 编辑:程序博客网 时间:2024/06/04 23:36

框架的基本结构:


先定义一个入口文件index.php.作用是1:定义常量;2:加载函数库;3:启动框架;

header("content-type:text/html;charset=utf-8");define('MULU',trim(str_replace('\\','/',__DIR__)));//定义框架所在目录define('CORE',MULU.'/core');//框架文件所在目录define('APP',MULU.'/app');//项目文件所处目录define('MODULE','app');//项目文件所处目录define('DEBUG',true);//是否开启调错模式if(DEBUG){    ini_set('display_errors','On');}else{    ini_set('display_errors','Off');}include CORE.'/common/function.php';//里面有一个打印的p方法include CORE.'/bawei.php';spl_autoload_register('\core\bawei::load');//自动加载类库\core\bawei::run();

core核心文件作用是加载类库

route里指明调用哪个控制器

namespace core;use route;class bawei{    public static $classMap=array();    static public function run(){        $route = new \core\lib\route();        $controllerClass=$route->controller;        $action=$route->action;        $controllerFile=APP.'/controller/'.$controllerClass.'Controller.php';        $controllerClass = '\\'.MODULE.'\controller\\'.$controllerClass.'Controller';        if(is_file($controllerFile)){            include $controllerFile;            $controller=new $controllerClass();            $controller->$action();        }else{            throw new \Exception('找不到控制器'.$controllerClass);        }    }    static public function load($class){            if(isset($classMap[$class])){                return true;            }else{                $class = str_replace('\\','/',$class);                $file=MULU.'/'.$class.'.php';                if(is_file($file)){                    include $file;                    self::$classMap[$class] = $class;                }else{                    return false;                }            }    }
lib里存放扩展类

namespace core\lib;class route{    public $controller;    public $action;    public function __construct(){        if(isset($_SERVER['REQUEST_URI'])&&$_SERVER['REQUEST_URI'] != '/'){            $path = $_SERVER['REQUEST_URI'];            $patharr = explode('/', trim($path, '/'));            if(isset($patharr[0])){                $this->controller=$patharr[0];            }            unset($patharr[0]);            if(isset($patharr[1])){                $this->action=$patharr[1];                unset($patharr[1]);            }else{                $this->action=config::get('ACTION','route');            }            $count=count($patharr)+2;            $i=2;            while($i<$count){                if(isset($patharr[$i+1])){                    $_GET[$patharr[$i]]=$patharr[$i+1];                }                $i=$i+2;                }            p($_GET);        }else{            $this->controller=config::get('CONTROLLER','route');            $this->action=config::get('ACTION','route');        }    }}

model里面加载数据库文件

namespace core\lib;use core\lib\config;class model extends \PDO{    public function __construct(){        $database=config::all("database");        try{            parent::__construct($database['DSN'],$database['USERNAME'],$database['PASSWORD']);        }catch (\PDOException $e){            p($e->getMessage());        }    }}

0 0
原创粉丝点击