Blogger Information
Blog 42
fans 5
comment 0
visits 38201
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mvc ,流程顺了,还是手生
张浩刚
Original
614 people have browsed it

model.php

  1. <?php
  2. namespace mvc;
  3. use PDO;
  4. class Model{
  5. public function __construct($a='mysql:host=localhost;dbname=film', $b='root', $c='root', $table='user')
  6. {
  7. $this->pdo = new PDO($a,$b,$c);
  8. $this->table = $table;
  9. }
  10. public function select(){
  11. $sql = "SELECT id,name,tel FROM {$this->table}";
  12. $stmt = $this->pdo->prepare($sql);
  13. $stmt->execute();
  14. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  15. }
  16. }

view.php

  1. <?php
  2. namespace mvc;
  3. class View{
  4. public function fetch($data){
  5. $table = '<table>';
  6. $table .= '<caption>商品列表</caption>';
  7. $table .= '<tr><th>ID</th><th>品名</th><th>型号</th></tr>';
  8. foreach($data as $v){
  9. $table .= '<tr>';
  10. $table .= '<td>' . $v['id'] . '</td>';
  11. $table .= '<td>' . $v['name'] . '</td>';
  12. $table .= '<td>' . $v['tel'] . '</td>';
  13. $table .= '</tr>';
  14. }
  15. $table .= '</table>';
  16. return $table;
  17. }
  18. }
  19. echo '<style>
  20. table {border-collapse: collapse; border: 1px solid; width: 500px;height: 170px}
  21. caption {font-size: 1.2rem; margin-bottom: 10px;}
  22. tr:first-of-type { background-color:lightblue;}
  23. td,th {border: 1px solid}
  24. td{text-align: center}
  25. </style>';

demo.php

  1. <?php
  2. namespace mvc;
  3. use Closure;
  4. require __DIR__ . '/model.php';
  5. require __DIR__ . '/view.php';
  6. //添加服务容器层
  7. class Container{
  8. //容器属性时一个空数组,里面装创建对象的方法
  9. protected $contt = [];
  10. // 放进去,把实例化过程绑定到容器中
  11. // $alias 类实类的别名 可以自定义;Closure用于代表 匿名函数 的类
  12. public function bind($alias, Closure $process){
  13. //将类实例化的方法 存储到容器中
  14. $this->contt[$alias] = $process;
  15. }
  16. //取出来,执行容器中的实例方法
  17. public function make($alias, $arr = []){
  18. //call_user_func_array 回调函数,第一个是回调函数,第二个时索引数组
  19. //如call_user_func_array($a, $b) 把($a)作为回调函数调用,把参数数组作($b)为回调函数的参数传入。
  20. return call_user_func_array($this->contt[$alias], $arr);
  21. }
  22. }
  23. $container = new Container();
  24. //把model的实例化过程,绑定到容器中
  25. $container->bind('model', function (){return new Model();});
  26. //把view的实例化过程,绑定到容器中
  27. $container->bind('view', function (){return new View();});
  28. //添加facade门面类
  29. class facade{
  30. protected static $container;
  31. protected static $data = [];
  32. // 导入服务器容器,并静态化
  33. public static function initialize(Container $container){
  34. static::$container = $container;
  35. }
  36. // 导入model,并静态化
  37. public static function getData(){
  38. static::$data = static::$container->make('model')->select();
  39. }
  40. // 导出view,并静态化
  41. public static function fetch(){
  42. return static::$container->make('view')->fetch(static::$data);
  43. }
  44. }
  45. //控制器
  46. class demo{
  47. public function __construct(Container $container)
  48. {
  49. facade::initialize($container);
  50. }
  51. public function getOut(){
  52. //获取数据
  53. facade::getData();
  54. //渲染模板
  55. return facade::fetch();
  56. }
  57. }
  58. //客户端
  59. $demo = new demo($container);
  60. echo $demo->getOut();

结果显示

手抄部分

总结

mvc对我来说,很有难度,做了几遍,流程基本懂了,但还是手生,主要问题在容器层,facade门面。。本周末好好练习

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!