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:
$str=$modelObj->getDate();
break;
case 2:
$str=$modelObj->getTime();
break;
default:
$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">
function confirmDel(id
)
{
if(window
.confirm('你真的要删除吗?'))
{
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=$_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()
{
$arr=array(
'db_host' => 'localhost',
'db_user' => 'root',
'db_pass' => '',
'db_name' => 'itcast',
'charset' => 'utf8'
);
$this->db=DB::getInstance($arr);
}
public function fetchAll()
{
$sql="select * from student";
return $this->db->fetchAll($sql);
}
public function delete($id)
{
$sql="delete from student where id={$id}";
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;
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();
$this->selectDb();
$this->setCharset();
}
private function __clone(){}
public static function getInstance($config=array())
{
if(!self
::$obj instanceof self)
{
self
::$obj=new self($config);
}
return self
::$obj;
}
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);
}
public function exec($sql)
{
$sql=strtolower($sql);
if(substr($sql,0,6)=='select')
{
echo "不能执行select语句!";
die();
}
return mysqli_query($this->link,$sql);
}
private function query($sql)
{
$sql=strtolower($sql);
if(substr($sql,0,6)!='select')
{
echo "不能执行非select语句!";
die();
}
return mysqli_query($this->link,$sql);
}
public function fetchOne($sql,$type=3)
{
$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)
{
$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)
{
$result=$this->query($sql);
return mysqli_num_rows($result);
}
public function __destruct()
{
mysqli_close($this->link);
}
}