Blogger Information
Blog 30
fans 1
comment 0
visits 16060
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mvc架构的运行原理,依赖注入与服务器容器
moon
Original
756 people have browsed it

MVC架构的运行原理

  • M: Model(模型层),最bottom一层,是核心数据层,程序需要操作的数据或信息.
  • V:View (视图层),最top一层,直接面向最终用户,视图层提供操作页面给用户,被誉为程序的外壳.
  • C: Controller(控制层),是middile层, 它负责根据用户从”视图层”输入的指令,选取”数据层”中的数据,然后对其进行相应的操作,产生最终结果。

MVC分离实现

  • 1.首先封装model层代码如下
  1. <?php
  2. namespace chloe\app;
  3. use PDO;
  4. class Model
  5. {
  6. public function getData()
  7. {
  8. return (new PDO('mysql:host=localhost;charset=utf8;dbname=phpcn', 'root', '', [PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING]))->query('SELECT `cou_id`,`title`,`pic`,`info`,`add_time`FROM `mj_course_lists` ORDER BY `cou_id` DESC LIMIT 6')->fetchAll();
  9. }
  10. public function editData()
  11. {
  12. }
  13. }
  • 2.view层实现
  1. <?php
  2. namespace chloe\app;
  3. class View
  4. {
  5. // $data渲染到视图层的数据
  6. public function fetch($data)
  7. {
  8. $table = '<table border="1" cellspacing="0" cellpadding="5" align="center">';
  9. $table .= '<caption>课程信息表</caption>';
  10. $table .= '
  11. <tr align="center">
  12. <td>编号</td>
  13. <td>名称</td>
  14. <td>封面</td>
  15. <td>课程简介</td>
  16. <td>创建时间</td>
  17. <td>操作</td>
  18. </tr>
  19. ';
  20. foreach ($data as $list) {
  21. $table .= '<tr>';
  22. $table .= '<td>' . $list['cou_id'] . '</td>';
  23. $table .= '<td>' . $list['title'] . '</td>';
  24. $table .= "<td><img style='width:100px' src='{$list['pic']}' alt='课程封面' ></td>";
  25. $table .= '<td>' . $list['info'] . '</td>';
  26. $table .= '<td>' . date("Y-m-d H:m:s", $list['add_time']) . '</td>';
  27. $table .= '<td><button>删除</button>&nbsp;<button>编辑</button> </td>';
  28. $table .= '</tr>';
  29. }
  30. $table .= '</table>';
  31. return $table;
  32. }
  33. }
  • 3.controller层实现
  1. <?php
  2. namespace chloe\app;
  3. // 类的自动加载
  4. require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
  5. // 中间层 controller 协调 m v
  6. class Controller
  7. {
  8. // 将外部依赖的对象 model view 在具体的操作方法中注入进来
  9. function index(Model $model, View $view)
  10. {
  11. // 获取数据
  12. $data = $model->getData();
  13. // 渲染数据到视图层
  14. return $view->fetch($data);
  15. }
  16. function edit(Model $model, View $view)
  17. {
  18. $model->editData();
  19. }
  20. }
  21. $model = new Model;
  22. $view = new View;
  23. echo (new Controller)->index($model, $view);

依赖注入

  • 1.类中的方法,需要依赖其他外部对象实现的方式,称之为依赖注入,下列代码展示了,依赖注入实现,controller依赖model,与view对象,
    model.php代码为
  1. <?php
  2. namespace chloe\app;
  3. use PDO;
  4. class Model
  5. {
  6. public function getData()
  7. {
  8. return (new PDO('mysql:host=localhost;charset=utf8;dbname=phpcn', 'root', '', [PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING]))->query('SELECT `cou_id`,`title`,`pic`,`info`,`add_time`FROM `mj_course_lists` ORDER BY `cou_id` DESC LIMIT 6')->fetchAll();
  9. }
  10. public function editData()
  11. {
  12. }
  13. }

view.php代码为

  1. <?php
  2. namespace chloe\app;
  3. class View
  4. {
  5. // $data渲染到视图层的数据
  6. public function fetch($data)
  7. {
  8. $table = '<table border="1" cellspacing="0" cellpadding="5" align="center">';
  9. $table .= '<caption>课程信息表</caption>';
  10. $table .= '
  11. <tr align="center">
  12. <td>编号</td>
  13. <td>名称</td>
  14. <td>封面</td>
  15. <td>课程简介</td>
  16. <td>创建时间</td>
  17. <td>操作</td>
  18. </tr>
  19. ';
  20. foreach ($data as $list) {
  21. $table .= '<tr>';
  22. $table .= '<td>' . $list['cou_id'] . '</td>';
  23. $table .= '<td>' . $list['title'] . '</td>';
  24. $table .= "<td><img style='width:100px' src='{$list['pic']}' alt='课程封面' ></td>";
  25. $table .= '<td>' . $list['info'] . '</td>';
  26. $table .= '<td>' . date("Y-m-d H:m:s", $list['add_time']) . '</td>';
  27. $table .= '<td><button>删除</button>&nbsp;<button>编辑</button> </td>';
  28. $table .= '</tr>';
  29. }
  30. $table .= '</table>';
  31. return $table;
  32. }
  33. }

controller.php代码为

  1. <?php
  2. namespace chloe\app;
  3. // 类的自动加载
  4. require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
  5. class Controller
  6. {
  7. // 将外部依赖的对象 model view 在具体的操作方法中注入进来
  8. function index(Model $model, View $view)
  9. {
  10. // 获取数据
  11. $data = $model->getData();
  12. // 渲染数据到视图层
  13. return $view->fetch($data);
  14. }
  15. function edit(Model $model, View $view)
  16. {
  17. $model->editData();
  18. }
  19. }
  20. $model = new Model;
  21. $view = new View;
  22. echo (new Controller)->index($model, $view);

服务器容器

  • 服务容器一个自动生产类 /对象的工厂,通过bind方法绑定类,通过make方法生成对象或者类,具体代码如下
  1. <?php
  2. namespace chloe\app;
  3. use Closure;
  4. // 服务容器 一个自动生产类 /对象的工厂
  5. class Container
  6. {
  7. // 对象容器
  8. protected $instances = [];
  9. /**
  10. * 绑定一个类标识、闭包、实例到容器
  11. * @access public
  12. * @param string|array $abstract 类标识或者接口的别名 alias
  13. * @param mixed $concrete 要绑定的类、闭包或者实例
  14. *
  15. */
  16. // 往容器数组中绑定对象
  17. public function bind($abstract, Closure $concrete)
  18. {
  19. $this->instances[$abstract] = $concrete;
  20. }
  21. public function make($abstract, $params = [])
  22. {
  23. return call_user_func_array($this->instances[$abstract], $params);
  24. }
  25. }
  26. $container = new Container;
  27. // 绑定一个闭包到服务容器中 我们使用$model对象时才去实例化
  28. $container->bind('model', function () {
  29. return new Model;
  30. });
  31. $container->bind('view', function () {
  32. return new View;
  33. });
  34. $container->make('model')->getData();
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