php mvc思想演示讀書筆記
在一個網頁上,根據使用者的請求(選擇),來顯示不同的時間效果:
效果1:只顯示年月日
效果2:只顯示時分秒
效果3(預設效果)顯示年月日時分秒;
第一
控制器:
Controller,是一個php檔案,由瀏覽器直接請求(訪問);
它需要做2件最核心的工作:
1,(根據請求),決定需要什麼資料,並去呼叫模型檔案(類),去獲取該資料;
2,(根據請求),決定需要將資料顯示在哪個檢視檔案中。
程式碼如下:
<?php
require './MyDateTime.class.php';//
//根據使用者請求,以決定“獲取”什麼樣的時間:
//效果1
if(!empty($_GET['f']) && $_GET['f'] == 'time'){
//$t = date("H:i:s");
$obj = new MyDateTime();
$t = $obj->GetTime();
}
//效果2
else if(!empty($_GET['f']) && $_GET['f'] == 'date'){
//$t = date("Y年m月d日");
$obj = new MyDateTime();
$t = $obj->GetDate();
}
else{//預設
//$t = date("Y年m月d日 H:i:s");
$obj = new MyDateTime();
$t = $obj->GetDateTime();
}
//表現(展示)該時間:
include './4mvc_time_demo1.html';
?>
第二步
模型:
Model,是一個php檔案,不能直接請求,只能“被載入”而發揮作用。
它的核心工作只有一個:
(根據控制器的要求)去生產資料;
MyDateTime.class.php
<?php
class MyDateTime{
function GetDate(){
return date("Y年m月d日");
}
function GetTime(){
return date("H:i:s");
}
function GetDateTime(){
return date("Y年m月d日 H:i:s");
}
}
?>
第三步
檢視:
View,是一個“偽html檔案”(因為其中有極簡單的php程式碼),它也不應由瀏覽器直接請求;
它的作用是:
結合html和css程式碼,顯示相應的變數(資料)
4mvc_time_demo1.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>網頁標題</title>
<meta name="keywords" content="關鍵字列表" />
<meta name="description" content="網頁描述" />
<link rel="stylesheet" type="text/css" href="" />
<style type="text/css">
.time{color:red; font-size:30px; background-color:yellow;}
</style>
<script type="text/javascript"></script>
</head>
<body>
<p align="right">
<a href="4mvc_time_demo1.php?f=date">形式1</a> |
<a href="4mvc_time_demo1.php?f=time">形式2</a> |
<a href="4mvc_time_demo1.php?f=datetime">形式3</a>
</p>
<div class="time">
<?php
echo $t;
?>
</div>
</body>
</html>