PHP简单的MVC框架demo
目录结构:

入口文件:index.php
<?php /** * 框架的入口文件 * 负责调用控制器 */ //接手收控制器名称 方法名 $controller = $_GET['c'].'Controller'; $action = $_GET['a']; //引入控制器 并实例化 require $controller. '.php'; $demo = new $controller(); //调用 $demo->$action();
控制器:demoController.php
<?php /** * wuyu * Class demoController * 控制器类 负责调用model操作 获取数据 渲染到view 模板页面上 */ class demoController { function index() { //查询数据 require 'model.php'; $model = new model('test'); $data = $model->getColumns('*')->from('a')->executeResults(); //页面渲染 require 'view.php'; $view = new view(); $view->display('template.php',$data); } }
数据库 类文件 model.php
<?php /** * Created by wuyu. * User: admin * Date: 2018/8/28 * Time: 21:47 */ class model{ private $pdo; public $log; private $sth; private $params = []; private $where; private $columns = '*'; private $tableName; function __construct($db) { switch ($db) { case 'flight': $dsn = DSN_FLT; $usr = USR_FLT; $pwd = PWD_FLT; break; case 'test': $dsn = 'mysql:host=localhost;dbname=test;charset=utf8'; $usr = 'root'; $pwd = ''; break; default: exit('no database: ' . $db); break; } try { $this->pdo = new PDO($dsn, $usr, $pwd); } catch(PDOException $e) { echo $this->log = LOG_TIME . "--connect error--" . $e->getMessage(); $this->pdo = false; } } /** * user:wuyu * @param $params * @return $this * 绑定参数 */ public function bindParams($params) { if (!empty($params)) { if (empty($this->params)) { $this->params = $params; } else { foreach ($params as $name => $value) { if (is_integer($name)) { $this->params[] = $value; } else { $this->params[$name] = $value; } } } } return $this; } /** * user:wuyu * @param $columns * @return $this * 查询 需要的字段 */ public function getColumns($columns){ $this->columns = $columns; return $this; } /** * user:wuyu * @param $tableName * @return $this * 设置表名称 */ public function from($tableName){ $this->tableName = $tableName; return $this; } /** * user:wuyu * @param $conditions * @param array $params * @return $this */ public function where($conditions,$params = []){ $this->where = $conditions; if(is_array($this->where)){ $this->where = implode(' and ',$this->where); } $this->bindParams($params); return $this; } /** * user:wuyu * @param $condition * @param array $params * @return $this */ public function andWhere($condition, $params = []) { if ($this->where === null) { $this->where = $condition; } else { if(is_array($condition)){ $condition = implode(' and ',$condition); } $this->where .= ' and '.$condition; } $this->bindParams($params); return $this; } /** * user:wuyu * @param $condition * @param array $params * @return $this */ public function orWhere($condition, $params = []) { if ($this->where === null) { $this->where = $condition; } else { if(is_array($condition)){ $condition = implode(' or ',$condition); } $this->where .= ' or '.$condition; } $this->bindParams($params); return $this; } /** * user:wuyu * @return mixed * 返回结果 */ public function executeResults(){ if($this->where){ $this->where .= ' where '.$this->where; }else{ $this->where = ''; } $sql = 'select '.$this->columns.' from '.$this->tableName.' ' .$this->where; $stmt = $this->pdo->prepare($sql); $stmt->execute($this->params); return $stmt->fetchAll(PDO::FETCH_ASSOC); } function __destruct() { $this->pdo = null; $this->sth = null; } }
模板渲染类 view.php
<?php /** * Created by PhpStorm. * User: admin * Date: 2018/8/28 * Time: 20:27 */ class view{ /** * @param $path * @param $data */ function display($path,$data){ require $path; } }
模板文件:template.php
<html>
<head>
<title>
测试
</title>
<body>
<?php
print_r($data) ;
?>
</body>
</head>
</html>

浙公网安备 33010602011771号