Blogger Information
Blog 34
fans 0
comment 0
visits 18408
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0517 mvc架构模式思想
千山暮雪
Original
503 people have browsed it

1.MVC (一种架构模式)

使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式。

  • M: Model(模型层),最bottom一层,是核心数据层,程序需要操作的数据或信息.
  • V:View (视图层),最top一层,直接面向最终用户,视图层提供操作页面给用户,被誉为程序的外壳.
  • 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 View
  3. {
  4. public function fetch($data)
  5. {
  6. $html = <<<EOF
  7. <table>
  8. <caption>用户信息表</caption>
  9. <thead>
  10. <tr>
  11. <td>用户名</td>
  12. <td>性别</td>
  13. </tr>
  14. </thead>
  15. <tbody>
  16. EOF;
  17. foreach ($data as $user) {
  18. $html .= "
  19. <tr>
  20. <td>{$user['username']}</td>
  21. <td>{$user['sex']}</td>
  22. </tr>";
  23. }
  24. $html .= " </tbody>
  25. </table>";
  26. return $html;
  27. }
  28. }

控制层-controller.php

  1. <?php
  2. require 'auto_load.php';
  3. class Controller
  4. {
  5. public function fetch()
  6. {
  7. $data = (new Model())->getData();
  8. echo (new View())->fetch($data);
  9. }
  10. }
  11. // 客户端
  12. (new Controller())->fetch();

2.容器与依赖注入

依赖注入是通过 php 的映射函数,解析到类在实例化的时候所依赖的类,直接将类实例化
容器对依赖注入的类\对象等进行统一接管的一个工厂

容器类-container.php

  1. class Container
  2. {
  3. // 1. 对象容器
  4. protected $instances = [];
  5. // 2. 绑定一个类、闭包、实例、接口实现到容器
  6. public function bind($abstract, Closure $concrete)
  7. {
  8. $this->instances[$abstract] = $concrete;
  9. }
  10. // 3. 调用容器中对象
  11. public function make($abstract, $params=[])
  12. {
  13. return call_user_func_array($this->instances[$abstract], $params);
  14. }
  15. }

控制器-controller3.php

  1. require 'auto_load.php';
  2. class Controller3
  3. {
  4. public function fetch(Container $container)
  5. {
  6. $data = $container->make('model')->getData();
  7. return $container->make('view')->fetch($data);
  8. }
  9. }
  10. $container = new Container();
  11. //绑定类到容器
  12. $container->bind('model', function () {
  13. return new Model();
  14. });
  15. $container->bind('view', function () {
  16. return new View();
  17. });
  18. // 输出
  19. echo (new Controller3())->fetch($container);

3.facade门面

Facade门面技术: 就是将服务容器中的对象的访问进行静态接管

Facade类 Facade.php

  1. class Facade
  2. {
  3. //为容器中的类提供一种静态调用方式
  4. protected static $container;
  5. public static function initialize(Container $container)
  6. {
  7. static::$container = $container;
  8. }
  9. }

UserModel类继承facade

  1. class UserModel extends Facade
  2. {
  3. public static function getData()
  4. {
  5. //静态绑定
  6. return static::$container->make('model')->getData();
  7. }
  8. }

UserView类继承facade

  1. class UserView extends Facade
  2. {
  3. public static function fetch($data)
  4. {
  5. //静态绑定
  6. return static::$container->make('view')->fetch($data);
  7. }
  8. }

控制器controller3.php

  1. require 'auto_load.php';
  2. class Controller2
  3. {
  4. public function __construct(Container $container)
  5. {
  6. Facade::initialize($container);
  7. }
  8. public function fetch()
  9. {
  10. $data = UserModel::getData();
  11. return UserView::fetch($data);
  12. }
  13. }
  14. $container = new Container;
  15. // 类绑定到容器
  16. $container->bind('model', function () {
  17. return new Model();
  18. });
  19. $container->bind('view', function () {
  20. return new View();
  21. });
  22. // 输出
  23. echo (new Controller2($container))->fetch();
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
Author's latest blog post