PHP学习笔记7——MVC框架思想原理

mac2026-08-28  13

PHP学习笔记7——MVC框架思想原理

MVC概述MVC各组件的功能简单案例11. 控制器文件Controller.php2. 模型类文件Modle.php3. 视图类文件View.php 简单实例2:学生信息管理系统(显示、删除学生)1.学生信息首页视图文件 StudentIndexView.html2. 学生控制器文件:StudentController.php3. 学生模型类文件:StudentModel.php4. 数据库工具类文件:Db.class.php

MVC概述

MVC:模型Model-视图View-控制器controller

MVC是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码。

MVC各组件的功能

Model(数据模型)用于处理应用程序数据逻辑的部分。通常模型对象负责在数据库中存在数据。View(视图)是用于处理数据显示的部分。通常视图是依据模型数据创建的。Controller(控制器)是应用程序中处理用户交互的部分。通常控制I器负责从视图读取数据,控制用户输入,并向模型发送数据。

简单案例1

1. 控制器文件Controller.php

<?php //包含模型类文件 require_once("./model.class.php"); //获取地址栏传递的参数 $type=isset($_GET['type'])?$_GET['type']:3; //创建模型类对象 $modelObj=new DateTime2(); //根据传递的不同客户参数,调用模型类的不同方法 switch ($type) { case 1: //调用模型类对象的getDate()方法 $str=$modelObj->getDate(); break; case 2: //调用模型类对象的getTime()方法 $str=$modelObj->getTime(); break; default: //调用模型类对象的getDateTime()方法 $str=$modelObj->getDateTime(); break; } //包含视图文件 include "./view.html";

2. 模型类文件Modle.php

<?php //定义日期时间类 class DateTime2 { //返回当前日期数据 public function getDate() { return date('Y-m-d'); } //返回当前时间数据 public function getTime() { return date('H:i:s'); } //返回当前日期时间数据 public function getDateTime() { return date('Y-m-d H:i:s'); } }

3. 视图类文件View.php

<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>MVC思想简单案例</title> </head> <body> <a href="?type=1">显示日期</a>| <a href="?type=2">显示时间</a>| <a href="?type=3">显示日期时间</a><hr> <h2>当前是:<font color="red"><?php echo $str?></font></h2> </body> </html>

简单实例2:学生信息管理系统(显示、删除学生)

1.学生信息首页视图文件 StudentIndexView.html

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>学生信息管理中心</title> <script type="text/javascript"> //定义JS的弹出确认框函数 function confirmDel(id) { if(window.confirm('你真的要删除吗?')) { //跳转地址:ac是action的简称,代表用户动作 location.href="?ac=delete&id="+id; } } </script> </head> <body> <div style="text-align:center;padding-bottom:10px;"> <h2>学生信息管理者中心</h2> <a href="#">添加学生</a> 共有<font color="red">234</font>个学生 </div> <table width="600" border="1" align="center" rules="all" cellpadding="5"> <tr bgcolor="#f0f0f0"> <th>编号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>操作选项</th> </tr> <?php foreach($arrs as $arr){?> <tr align="center"> <td><?php echo $arr['id']?></td> <td><?php echo $arr['name']?></td> <td><?php echo $arr['sex']?></td> <td><?php echo $arr['age']?></td> <td> <a href="">修改</a> | <a href="javascript:void(0)" onclick="confirmDel(<?php echo $arr['id']?>)">删除</a> </td> </tr> <?php }?> </table> </body> </html>

2. 学生控制器文件:StudentController.php

<?php //包含学生模型类 require_once("./StudentModel.class.php"); //获取用户动作参数 $ac=isset($_GET['ac'])?$_GET['ac']:''; //根据用户的不同动作,调用不同的方法 if($ac=='delete') { //获取地址栏传递id $id=$_GET['id']; //创建学生类对象 $modelObj=new StudentModel(); //判断是否删除成功 if($modelObj->delete($id)) { echo "<h2>id={$id}的记录删除成功!</h2>"; header("refresh:3&url=?"); die(); }else { echo "<h2>id={$id}的记录删除失败!</h2>"; header("refresh:3&url=?"); die(); } }else { //创建学生模型类对象 $modelObj=new StudentModel(); //获取多行数据 $arrs=$modelObj->fetchAll(); //包含学生首页视图文件 include "./StudentIndexView.html"; }

3. 学生模型类文件:StudentModel.php

<?php //包含数据库工具类 require_once("./Db.class.php"); //定义学生模型类 class StudentModel { //私有的保存数据库对象的属性 private $db=null; //构造函数 public function __construct() { //创建Db类对象 $arr=array( 'db_host' => 'localhost', 'db_user' => 'root', 'db_pass' => '', 'db_name' => 'itcast', 'charset' => 'utf8' ); $this->db=DB::getInstance($arr); } //获取多行数据 public function fetchAll() { //构建查询的SQL语句 $sql="select * from student"; //执行SQL语句,返回二维数组 return $this->db->fetchAll($sql); } //删除记录 public function delete($id) { //构建查删除的SQL语句 $sql="delete from student where id={$id}"; //执行SQL语句,返回二维数组 return $this->db->exec($sql); } }

4. 数据库工具类文件:Db.class.php

<?php //定义最终的单例的数据库工具类 class Db { //私有的静态的保存对象的属性 private static $obj=null; //私有的数据库配置信息 private $db_host;//主机名 private $db_user;//用户名 private $db_pass;//密码 private $db_name;//数据集名 private $charset;//字符集 private $link; //连接对象 //私有的构造方法:阻止类外new对象 private function __construct($config=array()) { $this->db_host=$config['db_host']; $this->db_user=$config['db_user']; $this->db_pass=$config['db_pass']; $this->db_name=$config['db_name']; $this->charset=$config['charset']; $this->connectDb();//连接mysql服务器 $this->selectDb();//选择数据库 $this->setCharset();//设置字符集 } //私有的克隆方法:阻止类外clone对象 private function __clone(){} //公共的静态的创建对象的方法 public static function getInstance($config=array()) { //判断当前对象是否存在 if(!self::$obj instanceof self) { //如果对象不存在,并保存它 self::$obj=new self($config); } //返回对象 return self::$obj; } //私有的连接mysql服务器方法 private function connectDb() { if(!$this->link=@mysqli_connect($this->db_host,$this->db_user,$this->db_pass)) { echo "连接mysql服务器失败!"; die(); } } //私有的选择数据库的方法 private function selectDb() { if(!mysqli_select_db($this->link,$this->db_name)) { echo "选择数据库{$this->db_name}失败!"; die(); } } //私有的设置字符集 private function setCharset() { mysqli_set_charset($this->link,$this->charset); } //公共的执行SQL的方法:insert 、update、delete、set、drop等 public function exec($sql) { //例如:update student set salary=salary+500 where id=5 //将SQL语句转成全小写 $sql=strtolower($sql); //判断是不是select语句 if(substr($sql,0,6)=='select') { echo "不能执行select语句!"; die(); } //返回执行的结果(布尔值) return mysqli_query($this->link,$sql); } //私有的执行SQL语句的方法:select //执行成功返回结果集对象,执行失败返回false private function query($sql) { //例如:select * from student //将SQL语句转成全小写 $sql=strtolower($sql); //判断是不是select语句 if(substr($sql,0,6)!='select') { echo "不能执行非select语句!"; die(); } //返回执行的结果(结果集) return mysqli_query($this->link,$sql); } //公共的获取单行数据的方法 public function fetchOne($sql,$type=3) { //执行SQL语句并返回结果集对象 $result=$this->query($sql); //定义返回数组类型的常量 $types=array( 1=>MYSQLI_NUM, 2=>MYSQLI_BOTH, 3=>MYSQLI_ASSOC ); //返回一维数组 return mysqli_fetch_array($result,$types[$type]); } //公共的获取多行数据的方法 public function fetchAll($sql,$type=3) { //执行SQL语句并返回结果集对象 $result=$this->query($sql); //定义返回数组类型的常量 $types=array( 1=>MYSQLI_NUM, 2=>MYSQLI_BOTH, 3=>MYSQLI_ASSOC ); //返回二维数组 return mysqli_fetch_all($result,$types[$type]); } //公共的获取记录数 public function rowCount($sql) { //执行SQL语句并返回结果集对象 $result=$this->query($sql); //返回记录数 return mysqli_num_rows($result); } //公共的析构方法 public function __destruct() { mysqli_close($this->link);//断开mysql连接 } }
最新回复(0)