Blogger Information
Blog 119
fans 3
comment 1
visits 94579
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
MVC框架(依赖注入、对象容器)
赵大叔
Original
558 people have browsed it

<!-- 作业内容:作业:1. mvc架构模式的运行原理是?三者各自的职责什么?请实例演绎m-v-c分离?2.你是如何理解依赖注入与服务容器的 -->

MVC 架构模式的运行原理与各职责

Model:模型类,数据库操作
View:视图类,展示到客户端
Controller:控制器,协调模型与视图

m-v-c 分离实例

Model 代码:

  1. <?php
  2. namespace mvc_demo;
  3. // Model 模型类: 用于数据库操作
  4. class Model
  5. {
  6. public function getData()
  7. {
  8. return (new \PDO('mysql:host=localhost;dbname=yllblroot', 'root','root'))
  9. ->query('SELECT * FROM `workplan0921` LIMIT 15')
  10. ->fetchAll(\PDO::FETCH_ASSOC);
  11. }
  12. }

View 代码:

  1. <?php
  2. namespace mvc_demo;
  3. // 视图类
  4. class View
  5. {
  6. public function fetch($data)
  7. {
  8. $table = '<table>';
  9. $table .= '<caption>重点工作计划进度明细</caption>';
  10. $table .= '<tr><th>STT</th><th>项目</th><th>计划</th><th>主责人</th><th>完成标准</th><th>开始时间</th><th>结束时间</th><th>完成时间</th><th>备注</th></tr>';
  11. // 将数据循环遍历出来
  12. foreach ($data as $plan) {
  13. $table .= '<tr>';
  14. $table .='<td>' .$plan['stt'] .'</td>';
  15. $table .='<td>' .$plan['project'] .'</td>';
  16. $table .='<td>' .$plan['plan'] .'</td>';
  17. $table .='<td>' .$plan['nguoiphtrach'] .'</td>';
  18. $table .='<td>' .date('Y-m-d', $plan['starttime']) .'</td>';
  19. $table .='<td>' .date('Y-m-d', $plan['endtime']) .'</td>';
  20. $table .='<td>' .date('Y-m-d', $plan['timehoanthanh']) .'</td>';
  21. $table .='<td>' .$plan['ghichu'] .'</td>';
  22. $table .= '</tr>';
  23. }
  24. $table .= '</table>';
  25. return $table;
  26. }
  27. }
  28. echo '<style>
  29. table {border-collapse: collapse; border: 1px solid;text-align: center; margin: auto;}
  30. caption {font-size: 1.2rem; margin-bottom: 10px;}
  31. tr:first-of-type { background-color:wheat;}
  32. td,th {border: 1px solid; padding:5px}
  33. </style>';

控制器一(普通注入):

  1. <?php
  2. namespace mvc_demo;
  3. // 控制器2
  4. // 1. 加载模型类
  5. require 'Model.php';
  6. // 2. 加载视图
  7. require 'View.php';
  8. // 3. 创建控制
  9. class Controller1
  10. {
  11. public function index(Model $model, View $view)
  12. {
  13. // 1. 获取数据
  14. $data = $model->getData();
  15. // 2. 渲染模板/视图
  16. return $view->fetch($data);
  17. }
  18. }
  19. // 客户端:
  20. $model = new Model;
  21. $view = new View;
  22. // 实例化控制器类
  23. $controller = new Controller1;
  24. echo $controller->index($model, $view);
  25. <?php
  26. namespace mvc_demo;
  27. // 控制器3
  28. // 1. 加载模型类
  29. require 'Model.php';
  30. // 2. 加载视图
  31. require 'View.php';
  32. // 3. 创建控制
  33. class Controller2
  34. {
  35. // 依赖对象属性: 对象参数
  36. private $model;
  37. private $view;
  38. public function __construct(Model $model, View $view)
  39. {
  40. $this->model = $model;
  41. $this->view = $view;
  42. }
  43. public function index1()
  44. {
  45. // 1. 获取数据
  46. $data = $this->model->getData();
  47. // 2. 渲染模板/视图
  48. return $this->view->fetch($data);
  49. }
  50. }
  51. // 客户端:
  52. $model = new Model;
  53. $view = new View;
  54. // 实例化控制器类
  55. $controller = new Controller2($model, $view);
  56. echo $controller->index1();

控制器三(对象容器):

  1. <?php
  2. namespace mvc_demo;
  3. // 1. 加载模型类
  4. require 'Model.php';
  5. // 2. 加载视图
  6. require 'View.php';
  7. // 3. 服务容器
  8. class Container
  9. {
  10. // 对象容器
  11. protected $instances = [];
  12. // 绑定: 向对象容器中添加一个类实例
  13. public function bind($alias, \Closure $process)
  14. {
  15. $this->instances[$alias] = $process;
  16. }
  17. // 取出: 从容器中取出一个类实例 (new)
  18. public function make($alias, $params = [])
  19. {
  20. return call_user_func_array($this->instances[$alias], []);
  21. }
  22. }
  23. $container = new Container;
  24. // 绑定
  25. $container->bind('model', function () {return new Model;});
  26. $container->bind('view', function () {return new View;});
  27. class Controller3
  28. {
  29. public function index(Container $container)
  30. {
  31. // 1. 获取数据
  32. $data = $container->make('model')->getData();
  33. // 2. 渲染模板/视图
  34. return $container->make('view')->fetch($data);
  35. }
  36. }
  37. // 客户端:
  38. // 实例化控制器类
  39. $controller = new Controller3();
  40. echo $controller->index($container);

Controller1:
[http://help10086.cn/0221/Controller1.php]
Controller2:
[http://help10086.cn/0221/Controller2.php]
Controller3:
[http://help10086.cn/0221/Controller3.php]

赖注入与服务容器

依赖注入:在客户端实例化类,把对象当做参数注入调用方法
容器:对象容器,对象管家

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
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
Author's latest blog post