Blogger Information
Blog 32
fans 2
comment 2
visits 23328
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
MVC框架模仿(0116)
暴风战斧
Original
474 people have browsed it

编程思路

这次作业主要是通过模仿老师的mvc实现!

作业总结

对于mvc我主要是理解和模仿,mvc肯定能干很多事情,但是现在我想不出来太多。我目前是这样理解的:MVC=Model+View+Controller,model作为“后台”,view是“前台”,controller是“搬运工”!

MVC代码与效果图

  • 模型model
  1. <?php
  2. //命名空间
  3. namespace Mvc;
  4. //模型类:用做数据库
  5. class Model
  6. {
  7. public function getData()
  8. {
  9. return [
  10. ['id' => 1, 'name' => 'Dell电脑', 'model' => 'xps', 'price' => 15000],
  11. ['id' => 2, 'name' => '华为手机', 'model' => 'P30 Pro', 'price' => 4988],
  12. ['id' => 3, 'name' => '小爱同学', 'model' => 'AI音箱', 'price' => 299],
  13. ['id' => 4, 'name' => '小米电视', 'model' => '液晶电视', 'price' => 2399]
  14. ];
  15. }
  16. }
  • 视图类view
  1. <?php
  2. namespace Mvc;
  3. //视图类:渲染页面
  4. class View
  5. {
  6. public function fetch($data)
  7. {
  8. $table = '<table>';
  9. $table .= '<caption>有钱就买清单</caption>';
  10. $table .= '<tr><td>ID</td><td>品名</td><td>型号</td><td>价格</td></tr>';
  11. foreach ($data as $good) {
  12. $table .= '<tr>';
  13. $table .= '<td>' . $good['id'] . '</td>';
  14. $table .= '<td>' . $good['name'] . '</td>';
  15. $table .= '<td>' . $good['model'] . '</td>';
  16. $table .= '<td>' . $good['price'] . '</td>';
  17. $table .= '</tr>';
  18. }
  19. $table .= '</table>';
  20. return $table;
  21. }
  22. }
  23. echo '<style>
  24. table {border-collapse: collapse; border: 1px solid; width: 500px;height: 150px}
  25. caption {font-size: 1.2rem; margin-bottom: 10px;}
  26. tr:first-of-type { background-color:lightblue;}
  27. td,th {border: 1px solid}
  28. td:first-of-type {text-align: center}
  29. </style>';
  • 控制器类controller
  1. <?php
  2. //控制器:将商品展示出来
  3. namespace Mvc;
  4. //1.加载模型类
  5. require 'Model.php';
  6. //2.加载视图类
  7. require 'View.php';
  8. //3.创建控制器类
  9. class Controller
  10. {
  11. //模型对象
  12. private $model;
  13. //视图对象
  14. private $view;
  15. //构造方法,作为外部对象注入点
  16. public function __construct($model,$view)
  17. {
  18. $this->model = $model;
  19. $this->view = $view;
  20. }
  21. public function index()
  22. {
  23. //1.获取数据
  24. $data = $this->model->getData();
  25. //2.渲染模板
  26. return $this->view->fetch($data);
  27. }
  28. }
  29. //4.客户端调用
  30. $model = new Model;
  31. $view = new View;
  32. //实例化控制器类
  33. //构造方法作为注入点,实现对外部以来对象的共享
  34. $contorller = new Controller($model,$view);
  35. echo $contorller->index();
  • 效果图

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:这个mvc只是基本的流程, 后面我们有更真实的案例
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments