Blogger Information
Blog 52
fans 1
comment 1
visits 38660
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mvc架构模式思想
小丑0o鱼
Original
425 people have browsed it
  1. 1.MVC (一种架构模式)
  2. 使用MVC的目的是将MV的实现代码分离,从而使同一个程序可以使用不同的表现形式。
  3. M: Model(模型层),最bottom一层,是核心数据层,程序需要操作的数据或信息.
  4. V:View (视图层),最top一层,直接面向最终用户,视图层提供操作页面给用户,被誉为程序的外壳.
  5. C: Controller(控制层),是middile层, 它负责根据用户从”视图层”输入的指令,选取”数据层”中的数据,然后对其进行相应的操作,产生最终结果。

模型层-model.php

  1. <?php
  2. class Model
  3. {
  4. public function getData()
  5. {
  6. $dsn = 'mysql:host=127.0.0.1;port=3306;dbname=user';
  7. $username = 'root';
  8. $password = 'root123';
  9. $pdo = new PDO($dsn, $username, $password);
  10. $sql = "select `username`,`sex` from `users` order by `id` asc limit 5";
  11. $stmt = $pdo->query($sql);
  12. $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
  13. return $res;
  14. }
  15. }

视图层-view.php

  1. <?php
  2. class Model
  3. {
  4. public function getData()
  5. {
  6. $dsn = 'mysql:host=127.0.0.1;port=3306;dbname=user';
  7. $username = 'root';
  8. $password = 'root123';
  9. $pdo = new PDO($dsn, $username, $password);
  10. $sql = "select `username`,`sex` from `users` order by `id` asc limit 5";
  11. $stmt = $pdo->query($sql);
  12. $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
  13. return $res;
  14. }
  15. }
  16. 视图层-view.php
  17. <?php
  18. class View
  19. {
  20. public function fetch($data)
  21. {
  22. $html = <<<EOF
  23. <table>
  24. <caption>用户信息表</caption>
  25. <thead>
  26. <tr>
  27. <td>用户名</td>
  28. <td>性别</td>
  29. </tr>
  30. </thead>
  31. <tbody>
  32. EOF;
  33. foreach ($data as $user) {
  34. $html .= "
  35. <tr>
  36. <td>{$user['username']}</td>
  37. <td>{$user['sex']}</td>
  38. </tr>";
  39. }
  40. $html .= " </tbody>
  41. </table>";
  42. return $html;
  43. }
  44. }
  1. 控制层-controller.php
  2. <?php
  3. require 'auto_load.php';
  4. class Controller
  5. {
  6. public function fetch()
  7. {
  8. $data = (new Model())->getData();
  9. echo (new View())->fetch($data);
  10. }
  11. }
  12. // 客户端
  13. (new Controller())->fetch();
  1. 2.容器与依赖注入
  2. 依赖注入是通过 php 的映射函数,解析到类在实例化的时候所依赖的类,直接将类实例化
  3. 容器对依赖注入的类\对象等进行统一接管的一个工厂
  4. 容器类-container.php
  5. class Container
  6. {
  7. // 1. 对象容器
  8. protected $instances = [];
  9. // 2. 绑定一个类、闭包、实例、接口实现到容器
  10. public function bind($abstract, Closure $concrete)
  11. {
  12. $this->instances[$abstract] = $concrete;
  13. }
  14. // 3. 调用容器中对象
  15. public function make($abstract, $params=[])
  16. {
  17. return call_user_func_array($this->instances[$abstract], $params);
  18. }
  19. }
  20. 控制器-controller3.php
  21. require 'auto_load.php';
  22. class Controller3
  23. {
  24. public function fetch(Container $container)
  25. {
  26. $data = $container->make('model')->getData();
  27. return $container->make('view')->fetch($data);
  28. }
  29. }
  30. $container = new Container();
  31. //绑定类到容器
  32. $container->bind('model', function () {
  33. return new Model();
  34. });
  35. $container->bind('view', function () {
  36. return new View();
  37. });
  38. // 输出
  39. echo (new Controller3())->fetch($container);
  40. 3.facade门面
  41. Facade门面技术: 就是将服务容器中的对象的访问进行静态接管
  42. Facade Facade.php
  43. class Facade
  44. {
  45. //为容器中的类提供一种静态调用方式
  46. protected static $container;
  47. public static function initialize(Container $container)
  48. {
  49. static::$container = $container;
  50. }
  51. }
  52. UserModel类继承facade
  53. class UserModel extends Facade
  54. {
  55. public static function getData()
  56. {
  57. //静态绑定
  58. return static::$container->make('model')->getData();
  59. }
  60. }
  61. UserView类继承facade
  62. class UserView extends Facade
  63. {
  64. public static function fetch($data)
  65. {
  66. //静态绑定
  67. return static::$container->make('view')->fetch($data);
  68. }
  69. }
  70. 控制器controller3.php
  71. require 'auto_load.php';
  72. class Controller2
  73. {
  74. public function __construct(Container $container)
  75. {
  76. Facade::initialize($container);
  77. }
  78. public function fetch()
  79. {
  80. $data = UserModel::getData();
  81. return UserView::fetch($data);
  82. }
  83. }
  84. $container = new Container;
  85. // 类绑定到容器
  86. $container->bind('model', function () {
  87. return new Model();
  88. });
  89. $container->bind('view', function () {
  90. return new View();
  91. });
  92. // 输出
  93. echo (new Controller2($container))->fetch();
Correction status:Uncorrected

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