博客列表 >PHP MVC模式实现数据库查询

PHP MVC模式实现数据库查询

Dong.
Dong.原创
2020年09月11日 15:25:481167浏览

1. MVC模式实现数据库查询

  • M: Model, 模型, 数据库的操作
  • V: View, 视图, 页面, html
  • C: Controller, 控制器
  • 仿站: V - M - C
  • 自主: M - V - C
  • 建用户列表,并连接数据库 —config.php

  1. <?php
  2. // 模型: 当前页面要显示的数据
  3. $pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'melinda123');
  4. $users = $pdo->query('select * from users limit 10')->fetchAll(PDO::FETCH_ASSOC);
  5. ?>
  6. <!-- 视图 -->
  7. <!DOCTYPE html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  12. <title>用户列表</title>
  13. </head>
  14. <body>
  15. <table border="1" width="60%">
  16. <caption>用户表</caption>
  17. <tr>
  18. <th>id</th>
  19. <th>name</th>
  20. <th>email</th>
  21. </tr>
  22. <!-- 渲染数据,用横版语法 -->
  23. <?php foreach ($users as $user): ?>
  24. <tr>
  25. <td><?=$user['id']?></td>
  26. <td><?=$user['name']?></td>
  27. <td><?=$user['email']?></td>
  28. </tr>
  29. <?php endforeach ?>
  30. </table>
  31. </body>
  32. </html>
  • 利用mvc模型完成项目 —connect.php

  1. <?php
  2. namespace mvc_demo;
  3. use PDO;
  4. // 模型: 数据库操作
  5. class Model
  6. {
  7. // 获取数据
  8. public function getData()
  9. {
  10. return (new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'melinda123'))
  11. ->query('select * from users limit 10')->fetchAll(PDO::FETCH_ASSOC);
  12. }
  13. }
  • 视图 —View.php

  1. <?php
  2. namespace mvc_demo;
  3. // 视图: 数据展示
  4. class View
  5. {
  6. // 数据展示
  7. public function fetch($data)
  8. {
  9. // 表格方式展示,使用字符串拼接实现table的html代码
  10. $table = '<table>';
  11. $table .= '<caption>用户信息表</caption>';
  12. $table .= '<tr><th>ID</th><th>用户名</th><th>邮箱</th></tr>';
  13. // 遍历用户表
  14. foreach ($data as $user){
  15. $table .= '<tr>';
  16. $table .= '<td>'.$user['id'].'</td>';
  17. $table .= '<td>'.$user['name'].'</td>';
  18. $table .= '<td>'.$user['email'].'</td>';
  19. $table .= '</tr>';
  20. }
  21. $table .= '</table>';
  22. return $table;
  23. }
  24. }
  25. echo '<style>
  26. table {border-collapse: collapse; border: 1px solid;text-align: center; width: 500px;height: 150px;width: 600px;}
  27. caption {font-size: 1.2rem; margin-bottom: 10px;}
  28. tr:first-of-type { background-color:yellow;}
  29. td,th {border: 1px solid; padding:5px}
  30. </style>';
  • 采用MVC架构项目 —demo1.php

  1. <?php
  2. // 控制器1
  3. namespace mvc_demo;
  4. // 加载模型类
  5. require 'Model.php';
  6. // 加载视图类
  7. require 'View.php';
  8. class Controller1
  9. {
  10. // 获取数据,并展示出来
  11. public function index()
  12. {
  13. // 1. 获取数据
  14. // 生成模型
  15. $model = new Model();
  16. // 调用方法
  17. $data = $model->getData();
  18. // 2. 渲染模板
  19. $view = new View();
  20. return $view->fetch($data);
  21. }
  22. }
  23. // 客户端调用(测试)
  24. // 创建控制器实例/对象
  25. $controller = new Controller1();
  26. echo $controller->index();

2. 服务容器

  1. <?php
  2. // 控制器5: Facade门面技术, 静态接管服务容器中的成员的访问
  3. namespace mvc_demo;
  4. use Closure;
  5. // 加载模型类
  6. require 'Model.php';
  7. // 加载视图类
  8. require 'View.php';
  9. // 服务容器
  10. class Container2
  11. {
  12. // 1. 对象容器
  13. protected $instances = [];
  14. // 2. 向对象容器中添加对象
  15. // 参数1: 是外部对象在当前对象容器数组中的键名/别名
  16. // 参数2: 是当前需要绑定到容器的对象的实例化过程(函数)
  17. public function bind($alias, Closure $process)
  18. {
  19. $this->instances[$alias] = $process;
  20. }
  21. // 3. 从对象容器中取出对象, 调用它
  22. public function make($alias, $params=[] ) {
  23. return call_user_func_array($this->instances[$alias], []);
  24. }
  25. }
  26. // 将外部对象: Model, View的实例绑定到服务容器中
  27. $container = new Container2;
  28. // 绑定模型类实例绑定到服务容器中
  29. $container->bind('model', function(){
  30. return new Model();
  31. });
  32. // 绑定视图类实例绑定到服务容器中
  33. $container->bind('view', function(){
  34. return new View();
  35. });
  36. // 在服务容器与工作的控制器之间再添加一个中间层: Facade
  37. class Facade
  38. {
  39. // 服务容器
  40. protected static $container = null;
  41. // 初始化方法: 就是给当前的Facade类扣$container属性赋值
  42. // 将外部的服务容器注入到当前的facade中
  43. public static function initialize(Container2 $container)
  44. {
  45. // 赋值
  46. static::$container = $container;
  47. }
  48. }
  49. // 模型类成员访问静态化(给成员套一个静态访问的马甲)
  50. class UserModel extends Facade
  51. {
  52. public static function getData()
  53. {
  54. return static::$container->make('model')->getData();
  55. }
  56. }
  57. // 视图类成员访问静态化(给成员套一个静态访问的马甲)
  58. class UserView extends Facade
  59. {
  60. public static function fetch($data)
  61. {
  62. return static::$container->make('view')->fetch($data);
  63. }
  64. }
  65. class Controller5
  66. {
  67. // 构造方法,初始化facade
  68. public function __construct(Container2 $container)
  69. {
  70. // 调用facade的container
  71. Facade::initialize($container);
  72. }
  73. // 用Facade方式类成员
  74. public function index()
  75. {
  76. // 1. 获取数据
  77. $data = UserModel::getData();
  78. // 2. 渲染模板
  79. return UserView::fetch($data);
  80. }
  81. }
  82. // 客户端调用(测试)
  83. $controller = new Controller5($container);
  84. echo $controller->index();
  85. 1.服务容器Container2
  86. 2.将外部对象: Model, View的实例绑定到服务容器中
  87. 3.中间层: Facade
  88. 4.外部服务容器注入到当前的facade
  89. 5.视图类成员访问静态化,静态访问马甲UserModel
  90. 6.视图类成员访问静态化,静态访问马甲UserView
  91. 7.工作控制器Controller5

总结

  • 如果依赖的对象非常多,项目中有许多对象和类要不断的调用,那么就需要制作一个容器,全部放进去
  • 服务容器作用:将当前类对许多外部对象的依赖, 转为对一个服务容器的依赖,使用服务容器进行了接管
  • Facade门面技术: 将对服务容器中的对象的访问进行接管(静态接管)
  • 用Facade方式类,获取数据,渲染模板,将页面展示
  • 设计模式:依赖注入,服务容器,Facade
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议