Blogger Information
Blog 24
fans 0
comment 0
visits 18761
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mvc以及服务容器、Facade门面技术
昔年
Original
892 people have browsed it

mvc以及服务容器

1.视图

  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>ID</th><th>名称</th><th>排序</th><th>父级商品</th></tr>';
  11. // 将数据循环遍历出来
  12. foreach ($data as $category) {
  13. $table .= '<tr>';
  14. $table .= '<td>' . $category['id'] . '</td>';
  15. $table .= '<td>' . $category['name'] . '</td>';
  16. $table .= '<td>' . ($category['sort'] ? '男' : '女') . '</td>';
  17. $table .= '<td>' . $category['parentid'] . '</td>';
  18. $table .= '</tr>';
  19. }
  20. $table .= '</table>';
  21. return $table;
  22. }
  23. }
  24. echo '<style>
  25. table {border-collapse: collapse; border: 1px solid;text-align: center; width: 500px;height: 150px;width: 600px;}
  26. caption {font-size: 1.2rem; margin-bottom: 10px;}
  27. tr:first-of-type { background-color:wheat;}
  28. td,th {border: 1px solid; padding:5px}
  29. </style>';
  30. // echo (new View)->fetch((new Model)->getData());

2.模型

  1. <?php
  2. namespace mvc_demo;
  3. use PDO;
  4. use Exception;
  5. //模型类,用于操作数据库
  6. class Model
  7. {
  8. public function getData(){
  9. $dsn= "mysql:host=127.0.0.1;dbname=phpedu";
  10. try {
  11. // 连接数据库
  12. $pdo=new PDO($dsn,'root','root');
  13. // var_dump($pdo);
  14. } catch (Exception $e) {
  15. die($e->getMessage());
  16. }
  17. return $pdo->query("select * from `category`")->fetchAll(PDO::FETCH_ASSOC);
  18. }
  19. }

3、MVC实现

3.1通过普通方法来实现对象依赖注入

  1. <?php
  2. namespace mvc_demo;
  3. //1.加载模型类
  4. require 'Model.php';
  5. //2.加载视图类
  6. require 'view.php';
  7. //3.创建控制
  8. class Controller2
  9. {
  10. public function index(Model $model, View $view)
  11. {
  12. $data = $model->getData();
  13. return $view->fetch($data);
  14. }
  15. }
  16. //客户端代码
  17. //实例化控制器
  18. $model = new Model;
  19. $view = new View;
  20. $controller = new Controller2;
  21. echo $controller->index($model,$view);

3.2通过构造方法实现对象依赖注入

  1. <?php
  2. namespace mvc_demo;
  3. //1.加载模型类
  4. require 'Model.php';
  5. //2.加载视图类
  6. require 'view.php';
  7. //3.创建控制
  8. class Controller3
  9. {
  10. private $model;
  11. private $view;
  12. public function __construct(Model $model,View $view)
  13. {
  14. $this->model = $model;
  15. $this->view=$view;
  16. }
  17. public function index()
  18. {
  19. $data = $this->model->getData();
  20. return $this->view->fetch($data);
  21. }
  22. }
  23. //客户端代码
  24. //实例化控制器
  25. $model = new Model;
  26. $view = new View;
  27. $controller = new Controller3($model,$view);
  28. echo $controller->index($model,$view);

3.3通过服务容器来管理对象

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

4.使用Facade门面技术结果对服务容器中的对象访问

  1. <?php
  2. namespace mvc_demo;
  3. use mvc_demo\Model as M;
  4. use mvc_demo\View as V;
  5. //1.加载模型类
  6. require 'Model.php';
  7. //2.加载视图类
  8. require 'view.php';
  9. //3.服务容器
  10. class Container2
  11. {
  12. protected $instance = [];
  13. //绑定:向对象容器中添加一个实例
  14. public function bind($alias, \Closure $process)
  15. {
  16. $this->instance[$alias] = $process;
  17. }
  18. //取出:从容器中取出一个实例
  19. public function make($alias, $param = [])
  20. {
  21. return call_user_func_array($this->instance[$alias], []);
  22. }
  23. public function destory($alias)
  24. {
  25. unset($this->instance[$alias]);
  26. }
  27. }
  28. $container = new Container2;
  29. $container->bind('model', function () {
  30. return new M;
  31. });
  32. $container->bind('view', function () {
  33. return new V;
  34. });
  35. /////////////////////////////////
  36. //4.Facade门面技术,可以接管对容器中的对象访问
  37. Class Facade
  38. {
  39. protected static $container = null;
  40. public static function initialize(Container2 $container){
  41. static::$container = $container;
  42. }
  43. }
  44. class Model extends Facade
  45. {
  46. protected static $data = [];
  47. public static function getData(){
  48. static::$data = static::$container->make('Model')->getData();
  49. }
  50. }
  51. class View extends Facade
  52. {
  53. public static function fetch()
  54. {
  55. return static::$container->make('view')->fetch();
  56. }
  57. }
  58. //////////////////////////////
  59. //3.创建控制
  60. class Controller5
  61. {
  62. public function __construct(Container2 $controller)
  63. {
  64. Facade::initialize($controller);
  65. }
  66. public function index()
  67. {
  68. $data = Model::getData();
  69. return View::fetch($data);
  70. }
  71. }
  72. //客户端代码
  73. //实例化控制器
  74. $container = new Container2;
  75. $controller = new Controller5($container);
  76. echo $controller->index();
Correcting teacher:天蓬老师天蓬老师

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