博主信息
博文 12
粉丝 0
评论 1
访问量 7269
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
8.6【简单的MVC操作流程】
小陈先生的博客
原创
863人浏览过

MVC拆分为三个部分;

M = model 数据层

V = view 视图层

C = controller 控制器

客户端HTTP请求=》路由route=》控制器controller=》数据层model=》渲染视图view=》返回给客户


model部分实例

<?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);
    }


}


view部分实例

<?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;
    }
}


controller部分实例

<?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();

运行实例 »

点击 "运行实例" 按钮查看在线实例

批改状态:合格

老师批语:写了这么代码 ,难道不想写一点总结吗? 有空看一下作业要求 , 这样的作业提交上来有什么意义呢? 作业给我看是其次, 最重要的是给自己的看
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
1条评论
小陈先生 2019-08-11 22:20:40
8月6日作业: 按照你的理解, 写一个简单的MVC操作流程的案例, 要求: 1. 使用真实的数据表中的数据; 2. 要求在构造方法中实现模型与视图的对象依赖注入 --我看了作业要求 也按照作业要求实现了啊 前面也有我自个的总结啊。。
1楼
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学