亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 40
粉絲 0
評(píng)論 1
訪問(wèn)量 49136
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
PHP MVC模式實(shí)現(xiàn)數(shù)據(jù)庫(kù)查詢
Dong.
原創(chuàng)
1393人瀏覽過(guò)

1. MVC模式實(shí)現(xiàn)數(shù)據(jù)庫(kù)查詢

  • M: Model, 模型, 數(shù)據(jù)庫(kù)的操作
  • V: View, 視圖, 頁(yè)面, html
  • C: Controller, 控制器
  • 仿站: V - M - C
  • 自主: M - V - C
  • 建用戶列表,并連接數(shù)據(jù)庫(kù) —config.php

  1. <?php
  2. // 模型: 當(dāng)前頁(yè)面要顯示的數(shù)據(jù)
  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. <!-- 渲染數(shù)據(jù),用橫版語(yǔ)法 -->
  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模型完成項(xiàng)目 —connect.php

  1. <?php
  2. namespace mvc_demo;
  3. use PDO;
  4. // 模型: 數(shù)據(jù)庫(kù)操作
  5. class Model
  6. {
  7. // 獲取數(shù)據(jù)
  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. // 視圖: 數(shù)據(jù)展示
  4. class View
  5. {
  6. // 數(shù)據(jù)展示
  7. public function fetch($data)
  8. {
  9. // 表格方式展示,使用字符串拼接實(shí)現(xiàn)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架構(gòu)項(xiàng)目 —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. // 獲取數(shù)據(jù),并展示出來(lái)
  11. public function index()
  12. {
  13. // 1. 獲取數(shù)據(jù)
  14. // 生成模型
  15. $model = new Model();
  16. // 調(diào)用方法
  17. $data = $model->getData();
  18. // 2. 渲染模板
  19. $view = new View();
  20. return $view->fetch($data);
  21. }
  22. }
  23. // 客戶端調(diào)用(測(cè)試)
  24. // 創(chuàng)建控制器實(shí)例/對(duì)象
  25. $controller = new Controller1();
  26. echo $controller->index();

2. 服務(wù)容器

  1. <?php
  2. // 控制器5: Facade門面技術(shù), 靜態(tài)接管服務(wù)容器中的成員的訪問(wèn)
  3. namespace mvc_demo;
  4. use Closure;
  5. // 加載模型類
  6. require 'Model.php';
  7. // 加載視圖類
  8. require 'View.php';
  9. // 服務(wù)容器
  10. class Container2
  11. {
  12. // 1. 對(duì)象容器
  13. protected $instances = [];
  14. // 2. 向?qū)ο笕萜髦刑砑訉?duì)象
  15. // 參數(shù)1: 是外部對(duì)象在當(dāng)前對(duì)象容器數(shù)組中的鍵名/別名
  16. // 參數(shù)2: 是當(dāng)前需要綁定到容器的對(duì)象的實(shí)例化過(guò)程(函數(shù))
  17. public function bind($alias, Closure $process)
  18. {
  19. $this->instances[$alias] = $process;
  20. }
  21. // 3. 從對(duì)象容器中取出對(duì)象, 調(diào)用它
  22. public function make($alias, $params=[] ) {
  23. return call_user_func_array($this->instances[$alias], []);
  24. }
  25. }
  26. // 將外部對(duì)象: Model, View的實(shí)例綁定到服務(wù)容器中
  27. $container = new Container2;
  28. // 綁定模型類實(shí)例綁定到服務(wù)容器中
  29. $container->bind('model', function(){
  30. return new Model();
  31. });
  32. // 綁定視圖類實(shí)例綁定到服務(wù)容器中
  33. $container->bind('view', function(){
  34. return new View();
  35. });
  36. // 在服務(wù)容器與工作的控制器之間再添加一個(gè)中間層: Facade
  37. class Facade
  38. {
  39. // 服務(wù)容器
  40. protected static $container = null;
  41. // 初始化方法: 就是給當(dāng)前的Facade類扣$container屬性賦值
  42. // 將外部的服務(wù)容器注入到當(dāng)前的facade中
  43. public static function initialize(Container2 $container)
  44. {
  45. // 賦值
  46. static::$container = $container;
  47. }
  48. }
  49. // 模型類成員訪問(wèn)靜態(tài)化(給成員套一個(gè)靜態(tài)訪問(wèn)的馬甲)
  50. class UserModel extends Facade
  51. {
  52. public static function getData()
  53. {
  54. return static::$container->make('model')->getData();
  55. }
  56. }
  57. // 視圖類成員訪問(wèn)靜態(tài)化(給成員套一個(gè)靜態(tài)訪問(wèn)的馬甲)
  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. // 構(gòu)造方法,初始化facade
  68. public function __construct(Container2 $container)
  69. {
  70. // 調(diào)用facade的container
  71. Facade::initialize($container);
  72. }
  73. // 用Facade方式類成員
  74. public function index()
  75. {
  76. // 1. 獲取數(shù)據(jù)
  77. $data = UserModel::getData();
  78. // 2. 渲染模板
  79. return UserView::fetch($data);
  80. }
  81. }
  82. // 客戶端調(diào)用(測(cè)試)
  83. $controller = new Controller5($container);
  84. echo $controller->index();
  85. 1.服務(wù)容器Container2
  86. 2.將外部對(duì)象: Model, View的實(shí)例綁定到服務(wù)容器中
  87. 3.中間層: Facade
  88. 4.外部服務(wù)容器注入到當(dāng)前的facade
  89. 5.視圖類成員訪問(wèn)靜態(tài)化,靜態(tài)訪問(wèn)馬甲UserModel
  90. 6.視圖類成員訪問(wèn)靜態(tài)化,靜態(tài)訪問(wèn)馬甲UserView
  91. 7.工作控制器Controller5

總結(jié)

  • 如果依賴的對(duì)象非常多,項(xiàng)目中有許多對(duì)象和類要不斷的調(diào)用,那么就需要制作一個(gè)容器,全部放進(jìn)去
  • 服務(wù)容器作用:將當(dāng)前類對(duì)許多外部對(duì)象的依賴, 轉(zhuǎn)為對(duì)一個(gè)服務(wù)容器的依賴,使用服務(wù)容器進(jìn)行了接管
  • Facade門面技術(shù): 將對(duì)服務(wù)容器中的對(duì)象的訪問(wèn)進(jìn)行接管(靜態(tài)接管)
  • 用Facade方式類,獲取數(shù)據(jù),渲染模板,將頁(yè)面展示
  • 設(shè)計(jì)模式:依賴注入,服務(wù)容器,F(xiàn)acade
批改老師:天蓬老師天蓬老師

批改狀態(tài):合格

老師批語(yǔ):其實(shí)代碼并不多, 但是其中的套路很有意思 , 值得多多體會(huì)
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
作者最新博文
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)