做一个简单的mvc封装

来源:互联网 发布:java 多线程 回调函数 编辑:程序博客网 时间:2024/05/22 08:33

先介绍一下MVC封装的思路:

首先需要一个入口文件,在入口文件里初始化核心类,在核心类中需要解析路由并自动加载路径,在控制器和模型层都需要去创建一个基类,在模型层基类里用pdo连接数据库并封装方法(接触的并不多,理解的也不太透彻,望各位大神能指教)。

接下来来写代码,比较算是囫囵吞枣的举例出来,没有介绍的很详细。

先创建一个mvc的文件夹,比对Thinkphp文件夹写首先创建一个入口文件index.php,在index.php里需要定义框架路径,项目路径及项目名称,开启调试模式,还有检测php 环境并加载框架入口文件flower.php(这些并没有按照创建mvc整体思路的一个流程写下来,而是直接贴了代码出来,后续的理解还是要靠自己了)。

<?php //定义常量define("PATH",realpath('./'));// echo PATH;die;define("F_PATH",PATH."/core");define("APP_PATH",PATH."/app");//开启调试模式define("DEBUG",true);if(DEBUG){ini_set("display_errors","on");}else{ini_set("display_errors","off");}//检测php环境if(version_compare(PHP_VERSION,'5.3.0','<'))  die('require PHP > 5.3.0 !');//开启框架require_once F_PATH.'/flower.php';spl_autoload_register('flower::autoLoad');flower::run();?>
另外创两个文件夹app和core(核心文件夹),在app下创建文件夹controller,model,view,在core下创建flower.php(框架入口文件)

<?php class flower{static public function run(){//路由$uri=$_SERVER["QUERY_STRING"];$controller="SiteController";$action="index";if(!empty($uri)){$data=explode("&",$uri);$c=explode("=",$data[0]);$a=explode("=",$data[1]);//首字母大写$controller = ucfirst($c[1])."Controller";$action = $a[1];}if(!class_exists($controller)){echo $controller." not found";die;}//实例化类$c=new $controller($controller,$action);$c->$action();//数据验证}//自动载入static public function autoLoad($class){$file= APP_PATH."/controller/".$class.".php";if(is_file($file)){include $file;}$file= APP_PATH."/model/".$class.".php";if(is_file($file)){include $file;}$file= F_PATH."/".$class.".php";if(is_file($file)){include $file;}}}?>

接下来具体话术描写就不太清晰了,毕竟我也是小菜鸟哈哈哈哈,就直接贴代码了

在controller文件夹下创建两个文件,DuanController.php,SiteController.php

<?php class DuanController{public function index(){echo "hello duanduan";}}?>

<?php header('Content-Type: text/html; charset=utf-8');class SiteController extends Controller{public function index(){$goodsModel=new GoodsModel();$list=$goodsModel->select();// var_dump($list);die;$this->assign('list',$list);$this->display();// echo "hello mvc";}}?>
在model 文件夹下创建GoodsModel.php,里面其实啥也没写,就创了一个空文件

<?php class GoodsModel extends model{}?>

在view文件夹下创建site文件夹 创建index.php,内容是<?=$list?>

重点来了,敲黑板,两个基类,先是Controller.php

<?php  class Controller{ public $arr; public $a; public $c; public function __construct($c,$a) { $this->c=$c; $this->a=$a; } function assign($name,$value) { $this->arr[$name]=$value; } function display($view="") { //extract()数组转变量。 extract($this->arr); //get_class返回用户定义的类名的小写形式 // echo get_class();die; // echo $this->c;die; $viewdir=strtolower(str_replace("Controller","",$this->c)); if(empty($view)) { $view = $this->a; } $file= APP_PATH."/view/".$viewdir."/".$view.".php"; if($file) { include $file; } }  }?>
这是model.php

<?php class model{public $table;public $pdo;function __construct(){$this->pdo=new PDO("mysql:host=localhost;dbname=tenth","root","root");$m=get_class($this);$this->table=strtolower(str_replace("Model", "", $m));}function select(){$rs=$this->pdo->query("select * from ".$this->table);$list=$rs->fetchAll(PDO::FETCH_ASSOC);return $list;}function find($id){$rs=$pdo->query("select * from ".$this->table." where id=".$id);$list=$rs->fetch(PDO::FETCH_ASSOC);return $list;}}?>

所有代码就贴在这了,理解要靠自己了,说实话我也有点不太理解,所以才把它记下来,希望各位大神加以指正