批改状态:合格
老师批语:写了这么代码 ,难道不想写一点总结吗? 有空看一下作业要求 , 这样的作业提交上来有什么意义呢? 作业给我看是其次, 最重要的是给自己的看
MVC拆分为三个部分;
M = model 数据层
V = view 视图层
C = controller 控制器
客户端HTTP请求=》路由route=》控制器controller=》数据层model=》渲染视图view=》返回给客户
<?php
namespace zuoye;
require __DIR__.'\utils\Db.php';
class Model
{
public function getDate(){
return utils\Db::table('staff')->select();
}
}
<?php
namespace zuoye\utils;
require 'Query.php';
class Db
{
protected static $pdo = null;
// 连接方法
public static function connection()
{
self::$pdo = new \PDO('mysql:host=127.0.0.1;dbname=php', 'root','root');
}
public static function __callStatic($name, $arguments)
{
// 连接数据库
self::connection();
// 实例 化一个查询类Query.php, table(), filed(), select()这些链式方法的提供者
$query = new Query(self::$pdo);
// 执行查询类中的方法
return call_user_func_array([$query, $name], $arguments);
}
}
<?php
namespace zuoye\utils;
class Query
{
// 连接对象
public $pdo = null;
// 数据表
public $table;
// 字段列表
public $field = '*';
// 查询条件
public $where;
// 显示数量
public $limit;
// 构造方法, 连接数据库
public function __construct($pdo)
{
$this->pdo = $pdo;
}
// 设置表名
public function table($tableName)
{
$this->table = $tableName;
// 关键步骤: 返回一个当前类的实例
return $this;
}
// 设置字段
public function field($fields = '*')
{
$this->field = empty($fields) ? '*' : $fields;
return $this;
}
// 设置查询条件
public function where($where = '')
{
$this->where = empty($where) ? $where : ' WHERE ' . $where;
return $this;
}
// 设置数量
public function limit($limit)
{
$this->limit = empty($limit) ? $limit : ' LIMIT ' . $limit;
return $this;
}
// 拼装SQL语句
public function select()
{
// 拼装
$sql = 'SELECT '
. $this->field // 字段
. ' FROM '
. $this->table // 数据表
. $this->where //条件
. $this->limit;
// 预处理查询
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
// die($stmt->debugDumpParams());
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
}<?php
/*
按照你的理解, 写一个简单的MVC操作流程的案例, 要求:
1. 使用真实的数据表中的数据;
2. 要求在构造方法中实现模型与视图的对象依赖注入
*/
namespace zuoye;
class View
{
public function show($data){
$table = '<table border="1" cellspacing="0" cellpadding="3" width="400">';
$table .= '<caption>人员信息表</caption>';
$table .= '<tr bgcolor="#add8e6"><th>ID</th><th>姓名</th><th>年龄</th><th>性别</th><th>职位</th></tr>';
// 遍历模型数据
foreach ($data as $staff) {
if($staff['sex']==1){
$staff['sexa'] = '男';
}elseif ($staff['sex']==0){
$staff['sexa'] = '女';
}
$table .= '<tr>';
$table .= '<td>' . $staff['staff_id'] . '</td>';
$table .= '<td>' . $staff['name'] . '</td>';
$table .= '<td>' . $staff['age'] . '</td>';
$table .= '<td>' . $staff['sexa'] . '</td>';
$table .= '<td>' . $staff['position'] . '</td>';
$table .= '</tr>';
}
$table .= '</table>';
return $table;
}
}<?php
namespace zuoye;
require 'Model.php';
require 'View.php';
class Controller
{
protected $model;
protected $view;
public function __construct(Model $model ,View $view)
{
$this->model = $model;
$this->view = $view;
}
public function index(){
return $this->view->show( $this->model->getDate());
}
}
echo (new Controller(new Model(),new View()))->index();点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号