闪电搭建自己MVC框架

 

1. 首先我们要有一个封装mvc的流程

2.创建和下图一样的文件夹,来封装mvc

 

3. 在入口文件index.PHP里面来写一些变量

 

[python] view plain copy
 
  1. <?php  
  2. /*  
  3.  * 入口文件  
  4.  * 1.定义常量  
  5.  * 2.加载函数库  
  6.  * 3.启动框架  
  7.  * */  
  8.   
  9. define('IMOOC',realpath('./'));  //当前框架所在的目录  
  10. define('CORE',IMOOC,'/core');  //框架核心文件所在的目录  
  11. define('APP',IMOOC,'/app');    //项目文件所在的目录  
  12. define('MODULE','app');  
  13.   
  14. define('DEBUG',true);  //开启调试模式  
  15.   
  16.   
  17. if(DEBUG)  
  18. {  
  19.     //打开显示错误的开关  
  20.     ini_set('display_error','On');  
  21. }else{  
  22.     ini_set('display_error','Off');  
  23. }  
  24.   
  25. include './core/common/function.php';   //加载函数库文件  
  26. //p(IMOOC);  
  27. include './core/imooc.php';   //加载框架的核心文件  
  28.   
  29. //当我们new的类不存在的时候会触发“spl_autoload_register('imooc::load');”这个方法  
  30. spl_autoload_register('\core\imooc::load');  
  31. \core\imooc::run();  
 

 

3.在入口文件里面写到了一个p方法,这个p方法是写在function.php里面的。(这个是我按照视频里面的写的)

 

[python] view plain copy
 
  1. function p($var)  
  2. {  
  3.     if(is_bool($var))  
  4.     {  
  5.         var_dump($var);  
  6.     }else if(is_null($var))  
  7.     {  
  8.         var_dump(NULL);  
  9.     }else{  
  10.         echo "<pre style='position:relative;z-index:1000;  
  11.               padding:10px;border-radius:5px;background:#F5F5F5;border:1px solid #aaa;  
  12.               font-size:14px;line-height:18px;opacity:0.9'>".print_r($var,true)."</pre>";  
  13.   
  14.     }  
  15. }  

 

 

4.创建一个lib文件夹来存放我们的路由类。

 

[python] view plain copy
 
  1. <?php  
  2. namespace core\lib;  
  3. class route  
  4. {  
  5.     public $controller;  
  6.     public $action;  
  7.     public function __construct()  
  8.     {  
  9.         //p('route ok');  
  10.         /*  
  11.          * 1.隐藏index.php  
  12.          * 2.获取URL的参数部分  
  13.          * 3.获取对应的控制器和方法  
  14.          * */  
  15.         //获取URL的参数部分  
  16.         if(isset($_SERVER['REQUEST_URI']) && $_SERVER["REQUEST_URI"] != '/')  
  17.         {  
  18.             //解析/index/index  
  19.             $path = $_SERVER['REQUEST_URI'];  
  20.             $patharr = explode('/',trim($path,'/'));  
  21.             //p($patharr);  
  22.             //判断是否存在  
  23.             if(isset($patharr[0]))  
  24.             {  
  25.                 $this->controller = $patharr[0];  
  26.             }  
  27.             unset($patharr[0]);  
  28.             if(isset($patharr[1]))  
  29.             {  
  30.                 $this->action = $patharr[1];  
  31.                 unset($patharr[1]);  
  32.             }else{  
  33.                 $this->action = 'index';  
  34.             }  
  35.             //url多余部分转换成GET参数  
  36.             $count = count($patharr) + 2;  
  37.             $i = 2;  
  38.             while($i<$count)  
  39.             {  
  40.                 if(isset($patharr[$i+1]))  
  41.                 {  
  42.                     $_GET[$patharr[$i]] = $patharr[$i+1];  
  43.                 }  
  44.                 $i = $i+2;  
  45.             }  
  46.             unset($_GET['url']);  
  47.         }else{  
  48.             //默认情况下控制器是index  
  49.             $this->controller = 'index';  
  50.             //默认情况下方法是index  
  51.             $this->action = 'index';  
  52.     }  
  53.   
  54.     }  
  55. }  



 

5.在imooc.php里面写控制器的类和一个自动加载类

 

[python] view plain copy
 
  1. <?php  
  2. /**  
  3.  * Created by PhpStorm.  
  4.  * User: pc  
  5.  * Date: 2016/12/23  
  6.  * Time: 18:44  
  7.  */  
  8. namespace core;  
  9.   
  10. class imooc  
  11. {  
  12.     public static $classMap = array();  
  13.     public $assign;  
  14.   
  15.     //加载控制器  
  16.     static public function run()  
  17.     {  
  18.         //p('OK');  
  19.         $route = new \core\lib\route();  
  20.         $controllerClass = $route->controller;  
  21.         $action = $route->action;  
  22.         $controllerfile = 'app/controller/'.$controllerClass.'Controller.php';  
  23.         //p($controllerfile);exit();  
  24.         $controllerClass = '\\'.MODULE.'\controller\\'.$controllerClass.'Controller';  
  25.         if(is_file($controllerfile))  
  26.         {  
  27.             include $controllerfile;  
  28.             $controller = new $controllerClass();  
  29.             $controller -> $action();  
  30.         }else{  
  31.             throw new \Exception('找不到控制器'.$controllerClass);  
  32.         }  
  33.         //p($route);  
  34.   
  35.     }  
  36.   
  37.     //自动加载类  
  38.     static public function load($class)  
  39.     {  
  40.         //自动加载类库  
  41.         if(isset($classMap[$class]))  
  42.         {  
  43.             return true;  
  44.         }else{  
  45.             $class = str_replace('\\','/',$class);  
  46.             $file = IMOOC.'/'.$class.'.php';  
  47.             if(is_file($file))  
  48.             {  
  49.                 include $file;  
  50.                 self::$classMap[$class] = $class;  
  51.             }else{  
  52.                 return false;  
  53.             }  
  54.         }  
  55.   
  56.     }  

 

 

 

6.然后在lib里面创建一个模型类model.php来连接我们数据库

 

[php] view plain copy
 
  1. <?php  
  2. namespace core\lib;  
  3.   
  4. class model extends \PDO  
  5. {  
  6.     public function __construct()  
  7.     {  
  8.         $dsn = 'mysql:host=localhost;dbname=mvc';  
  9.         $username = 'root';  
  10.         $password = 'root';  
  11.         try{  
  12.             parent::__construct($dsn,$username,$password);  
  13.         }catch (\PDOException $e){  
  14.             p($e->getMessage());  
  15.         }  
  16.     }  
  17. }  

 

模型写好后,在控制器层里面来调用

 

[php] view plain copy
 
  1. <?php  
  2. namespace app\controller;  
  3. class indexController extends \core\imooc  
  4. {  
  5.     public function index()  
  6.     {  
  7.         $model = new \core\lib\model();  
  8.         $sql = "SELECT * FROM user ";  
  9.         $res = $model->query($sql);  
  10.         p($res->fetchAll());  
  11.   
  12.     }  
  13.   
  14. }  


然后这样就可以连接我们的数据库了。

 

7.然后就是最后一个了,创建一个视图层views文件夹在里面创建一个index,html文件,index.html里面的内容可以随便写。例如

 

[html] view plain copy
 
  1. <h1><?php echo $title;?></h1>  
  2. <h3><?php echo $data;?></h3>  


然后就是在控制器层里面来写

 

 

[php] view plain copy
 
  1. <?php  
  2. namespace app\controller;  
  3. class indexController extends \core\imooc  
  4. {  
  5.     public function index()  
  6.     {  
  7.         $data = "hello world";  
  8.         $title = "视图文件";  
  9.         $this -> assign('title',$title);  
  10.         $this -> assign('data',$data);  
  11.         $this -> dispaly('index.html');  
  12.       
  13.     }  
  14.   
  15. }  

 

然后就是assign和display还有data是怎么来的,是写在imooc文件里面的

 

[php] view plain copy
 
  1. public function assign($name,$value)  
  2.     {  
  3.         $this->assign[$name] = $value;  
  4.     }  
  5.   
  6.     public function dispaly($file)  
  7.     {  
  8.         $file = 'app/views/'.$file;  
  9.         if(is_file($file))  
  10.         {  
  11.             extract($this->assign);  
  12.             include $file;  
  13.         }  
  14.     }  


但是这样还不行,我们还要在里面写一个属性值来存放值

 

 

[php] view plain copy
 
  1. public $assign;  


这样一个简单的mvc框架就已经封装好了!!!

 

posted @ 2017-08-15 09:51  QuanZhiGuo的博客  阅读(679)  评论(0编辑  收藏  举报