PHP MVC简单笔记
index.php入口文件
<?php
require_once "Controller.php";
Controller.php控制器
<?php
//包含模型类文件,注意是先引用模型后引用视图
require_once ("model.class.php");
//获取客户传递的参数,给个默认值为3
$type= isset($_GET['type']) ? $_GET['type'] : 3;
//调用 model
$modelObj = new DateTime2();
switch ($type){
case 1:
$str = $modelObj->getDate();
break;
case 2:
$str=$modelObj->getTime();
break;
default:
$str=$modelObj->getDateTime();
break;
}
//调用view,注意是后引用视图,先引用模型
include "view.html";
model.class.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");
}
}
View.html 视图文件
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<a href="?type=1">显示日期</a>|
<a href="?type=2">显示时间</a>|
<a href="?type=3">显示日期时间</a>
<hr>当前是:
<?php
echo $str;
?>
</body>
</html>