
批改狀態(tài):合格
老師批語(yǔ):其實(shí)代碼并不多, 但是其中的套路很有意思 , 值得多多體會(huì)
- M: Model, 模型, 數(shù)據(jù)庫(kù)的操作
- V: View, 視圖, 頁(yè)面, html
- C: Controller, 控制器
- 仿站: V - M - C
- 自主: M - V - C
<?php
// 模型: 當(dāng)前頁(yè)面要顯示的數(shù)據(jù)
$pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'melinda123');
$users = $pdo->query('select * from users limit 10')->fetchAll(PDO::FETCH_ASSOC);
?>
<!-- 視圖 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用戶列表</title>
</head>
<body>
<table border="1" width="60%">
<caption>用戶表</caption>
<tr>
<th>id</th>
<th>name</th>
<th>email</th>
</tr>
<!-- 渲染數(shù)據(jù),用橫版語(yǔ)法 -->
<?php foreach ($users as $user): ?>
<tr>
<td><?=$user['id']?></td>
<td><?=$user['name']?></td>
<td><?=$user['email']?></td>
</tr>
<?php endforeach ?>
</table>
</body>
</html>
<?php
namespace mvc_demo;
use PDO;
// 模型: 數(shù)據(jù)庫(kù)操作
class Model
{
// 獲取數(shù)據(jù)
public function getData()
{
return (new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'melinda123'))
->query('select * from users limit 10')->fetchAll(PDO::FETCH_ASSOC);
}
}
<?php
namespace mvc_demo;
// 視圖: 數(shù)據(jù)展示
class View
{
// 數(shù)據(jù)展示
public function fetch($data)
{
// 表格方式展示,使用字符串拼接實(shí)現(xiàn)table的html代碼
$table = '<table>';
$table .= '<caption>用戶信息表</caption>';
$table .= '<tr><th>ID</th><th>用戶名</th><th>郵箱</th></tr>';
// 遍歷用戶表
foreach ($data as $user){
$table .= '<tr>';
$table .= '<td>'.$user['id'].'</td>';
$table .= '<td>'.$user['name'].'</td>';
$table .= '<td>'.$user['email'].'</td>';
$table .= '</tr>';
}
$table .= '</table>';
return $table;
}
}
echo '<style>
table {border-collapse: collapse; border: 1px solid;text-align: center; width: 500px;height: 150px;width: 600px;}
caption {font-size: 1.2rem; margin-bottom: 10px;}
tr:first-of-type { background-color:yellow;}
td,th {border: 1px solid; padding:5px}
</style>';
<?php
// 控制器1
namespace mvc_demo;
// 加載模型類
require 'Model.php';
// 加載視圖類
require 'View.php';
class Controller1
{
// 獲取數(shù)據(jù),并展示出來(lái)
public function index()
{
// 1. 獲取數(shù)據(jù)
// 生成模型
$model = new Model();
// 調(diào)用方法
$data = $model->getData();
// 2. 渲染模板
$view = new View();
return $view->fetch($data);
}
}
// 客戶端調(diào)用(測(cè)試)
// 創(chuàng)建控制器實(shí)例/對(duì)象
$controller = new Controller1();
echo $controller->index();
<?php
// 控制器5: Facade門面技術(shù), 靜態(tài)接管服務(wù)容器中的成員的訪問(wèn)
namespace mvc_demo;
use Closure;
// 加載模型類
require 'Model.php';
// 加載視圖類
require 'View.php';
// 服務(wù)容器
class Container2
{
// 1. 對(duì)象容器
protected $instances = [];
// 2. 向?qū)ο笕萜髦刑砑訉?duì)象
// 參數(shù)1: 是外部對(duì)象在當(dāng)前對(duì)象容器數(shù)組中的鍵名/別名
// 參數(shù)2: 是當(dāng)前需要綁定到容器的對(duì)象的實(shí)例化過(guò)程(函數(shù))
public function bind($alias, Closure $process)
{
$this->instances[$alias] = $process;
}
// 3. 從對(duì)象容器中取出對(duì)象, 調(diào)用它
public function make($alias, $params=[] ) {
return call_user_func_array($this->instances[$alias], []);
}
}
// 將外部對(duì)象: Model, View的實(shí)例綁定到服務(wù)容器中
$container = new Container2;
// 綁定模型類實(shí)例綁定到服務(wù)容器中
$container->bind('model', function(){
return new Model();
});
// 綁定視圖類實(shí)例綁定到服務(wù)容器中
$container->bind('view', function(){
return new View();
});
// 在服務(wù)容器與工作的控制器之間再添加一個(gè)中間層: Facade
class Facade
{
// 服務(wù)容器
protected static $container = null;
// 初始化方法: 就是給當(dāng)前的Facade類扣$container屬性賦值
// 將外部的服務(wù)容器注入到當(dāng)前的facade中
public static function initialize(Container2 $container)
{
// 賦值
static::$container = $container;
}
}
// 模型類成員訪問(wèn)靜態(tài)化(給成員套一個(gè)靜態(tài)訪問(wèn)的馬甲)
class UserModel extends Facade
{
public static function getData()
{
return static::$container->make('model')->getData();
}
}
// 視圖類成員訪問(wèn)靜態(tài)化(給成員套一個(gè)靜態(tài)訪問(wèn)的馬甲)
class UserView extends Facade
{
public static function fetch($data)
{
return static::$container->make('view')->fetch($data);
}
}
class Controller5
{
// 構(gòu)造方法,初始化facade
public function __construct(Container2 $container)
{
// 調(diào)用facade的container
Facade::initialize($container);
}
// 用Facade方式類成員
public function index()
{
// 1. 獲取數(shù)據(jù)
$data = UserModel::getData();
// 2. 渲染模板
return UserView::fetch($data);
}
}
// 客戶端調(diào)用(測(cè)試)
$controller = new Controller5($container);
echo $controller->index();
1.服務(wù)容器Container2
2.將外部對(duì)象: Model, View的實(shí)例綁定到服務(wù)容器中
3.中間層: Facade
4.外部服務(wù)容器注入到當(dāng)前的facade中
5.視圖類成員訪問(wèn)靜態(tài)化,靜態(tài)訪問(wèn)馬甲UserModel
6.視圖類成員訪問(wèn)靜態(tài)化,靜態(tài)訪問(wèn)馬甲UserView
7.工作控制器Controller5
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)