1. 首先我们要有一个封装mvc的流程
2.创建和下图一样的文件夹,来封装mvc
3. 在入口文件index.PHP里面来写一些变量
[python] view plain copy <?php /* * 入口文件 * 1.定义常量 * 2.加载函数库 * 3.启动框架 * */ define('IMOOC',realpath('./')); //当前框架所在的目录 define('CORE',IMOOC,'/core'); //框架核心文件所在的目录 define('APP',IMOOC,'/app'); //项目文件所在的目录 define('MODULE','app'); define('DEBUG',true); //开启调试模式 if(DEBUG) { //打开显示错误的开关 ini_set('display_error','On'); }else{ ini_set('display_error','Off'); } include './core/common/function.php'; //加载函数库文件 //p(IMOOC); include './core/imooc.php'; //加载框架的核心文件 //当我们new的类不存在的时候会触发“spl_autoload_register('imooc::load');”这个方法 spl_autoload_register('\core\imooc::load'); \core\imooc::run();
3.在入口文件里面写到了一个p方法,这个p方法是写在function.php里面的。(这个是我按照视频里面的写的)
[python] view plain copy function p($var) { if(is_bool($var)) { var_dump($var); }else if(is_null($var)) { var_dump(NULL); }else{ echo "<pre style='position:relative;z-index:1000; padding:10px;border-radius:5px;background:#F5F5F5;border:1px solid #aaa; font-size:14px;line-height:18px;opacity:0.9'>".print_r($var,true)."</pre>"; } }
4.创建一个lib文件夹来存放我们的路由类。
[python] view plain copy <?php namespace core\lib; class route { public $controller; public $action; public function __construct() { //p('route ok'); /* * 1.隐藏index.php * 2.获取URL的参数部分 * 3.获取对应的控制器和方法 * */ //获取URL的参数部分 if(isset($_SERVER['REQUEST_URI']) && $_SERVER["REQUEST_URI"] != '/') { //解析/index/index $path = $_SERVER['REQUEST_URI']; $patharr = explode('/',trim($path,'/')); //p($patharr); //判断是否存在 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 = 'index'; } //url多余部分转换成GET参数 $count = count($patharr) + 2; $i = 2; while($i<$count) { if(isset($patharr[$i+1])) { $_GET[$patharr[$i]] = $patharr[$i+1]; } $i = $i+2; } unset($_GET['url']); }else{ //默认情况下控制器是index $this->controller = 'index'; //默认情况下方法是index $this->action = 'index'; } } }
5.在imooc.php里面写控制器的类和一个自动加载类
[python] view plain copy <?php /** * Created by PhpStorm. * User: pc * Date: 2016/12/23 * Time: 18:44 */ namespace core; class imooc { public static $classMap = array(); public $assign; //加载控制器 static public function run() { //p('OK'); $route = new \core\lib\route(); $controllerClass = $route->controller; $action = $route->action; $controllerfile = 'app/controller/'.$controllerClass.'Controller.php'; //p($controllerfile);exit(); $controllerClass = '\\'.MODULE.'\controller\\'.$controllerClass.'Controller'; if(is_file($controllerfile)) { include $controllerfile; $controller = new $controllerClass(); $controller -> $action(); }else{ throw new \Exception('找不到控制器'.$controllerClass); } //p($route); } //自动加载类 static public function load($class) { //自动加载类库 if(isset($classMap[$class])) { return true; }else{ $class = str_replace('\\','/',$class); $file = IMOOC.'/'.$class.'.php'; if(is_file($file)) { include $file; self::$classMap[$class] = $class; }else{ return false; } } }
6.然后在lib里面创建一个模型类model.php来连接我们数据库
[php] view plain copy <?php namespace core\lib; class model extends \PDO { public function __construct() { $dsn = 'mysql:host=localhost;dbname=mvc'; $username = 'root'; $password = 'root'; try{ parent::__construct($dsn,$username,$password); }catch (\PDOException $e){ p($e->getMessage()); } } }
模型写好后,在控制器层里面来调用
[php] view plain copy <?php namespace app\controller; class indexController extends \core\imooc { public function index() { $model = new \core\lib\model(); $sql = "SELECT * FROM user "; $res = $model->query($sql); p($res->fetchAll()); } }
然后这样就可以连接我们的数据库了。
7.然后就是最后一个了,创建一个视图层views文件夹在里面创建一个index,html文件,index.html里面的内容可以随便写。例如
[html] view plain copy <h1><?php echo $title;?></h1> <h3><?php echo $data;?></h3>
然后就是在控制器层里面来写
[php] view plain copy <?php namespace app\controller; class indexController extends \core\imooc { public function index() { $data = "hello world"; $title = "视图文件"; $this -> assign('title',$title); $this -> assign('data',$data); $this -> dispaly('index.html'); } }
然后就是assign和display还有data是怎么来的,是写在imooc文件里面的
[php] view plain copy public function assign($name,$value) { $this->assign[$name] = $value; } public function dispaly($file) { $file = 'app/views/'.$file; if(is_file($file)) { extract($this->assign); include $file; } }
但是这样还不行,我们还要在里面写一个属性值来存放值
[php] view plain copy public $assign;
这样一个简单的mvc框架就已经封装好了!!!
转载于:https://www.cnblogs.com/quanzhiguo/p/7363491.html