Blogger Information
Blog 32
fans 1
comment 0
visits 23130
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1206作业迷你MVC框架-PHP培训第九期线上班
淡月
Original
898 people have browsed it

1model

  1. <?php
  2. namespace __1206;
  3. // 模型类:用于数据表的操作
  4. class Model{
  5. public function getData()
  6. {
  7. // 用二维数组来模拟从表中获取到的客户数据
  8. return [
  9. ['id'=>1,'name'=>'不甜的淡月','sex'=>'女','mobile'=>'452557923','email'=>'452557923@qq.com','vip'=>'璀璨钻石'],
  10. ['id'=>2,'name'=>'溪上花间','sex'=>'女','mobile'=>'437366515','email'=>'437366515@qq.com','vip'=>'华贵铂金'],
  11. ['id'=>3,'name'=>'小小挽歌','sex'=>'男','mobile'=>'1047826346','email'=>'104782346@qq.com','vip'=>'荣耀黄金'],
  12. ['id'=>4,'name'=>'樱桃小丸子','sex'=>'男','mobile'=>'2244423838','email'=>'2244423838@qq.com','vip'=>'不屈白银'],
  13. ['id'=>5,'name'=>'梦幻泡沫','sex'=>'男','mobile'=>'2502704500','email'=>'2502704500@qq.com','vip'=>'英勇黄铜'],
  14. ];
  15. }
  16. }

2view

  1. <?php
  2. namespace __1206;
  3. // 视图类:渲染数据
  4. class View{
  5. public function fetch($data){
  6. $table = '<table>';
  7. $table .= '<caption>公交车群内成员信息</caption>';
  8. $table .= '<tr><th>ID</th><th>昵称</th><th>性别</th><th>联系方式</th><th>邮箱</th><th>段位等级</th></tr>';
  9. foreach ($data as $news) {
  10. $table .= '<tr>';
  11. $table .= '<td>' . $news['id'] . '</td>';
  12. $table .= '<td>' . $news['name'] . '</td>';
  13. $table .= '<td>' . $news['sex'] . '</td>';
  14. $table .= '<td>' . $news['mobile'] . '</td>';
  15. $table .= '<td>' . $news['email'] . '</td>';
  16. $table .= '<td>' . $news['vip'] . '</td>';
  17. $table .= '</tr>';
  18. }
  19. $table .= '</table>';
  20. return $table;
  21. }
  22. }
  23. echo '<style>
  24. table {border-collapse: collapse; border: 1px solid; width: 600px;height: 200px}
  25. caption {font-size: 1.2rem; margin-bottom: 10px;}
  26. tr:first-of-type { background-color:pink;}
  27. td,th {border: 1px solid}
  28. td:first-of-type {text-align: center;}
  29. </style>';

3controller

  1. <?php
  2. // 控制器: 将客户信息表展示出来
  3. namespace __1206;
  4. // 1. 加载模型
  5. require 'Model.php';
  6. // 2. 加载视图
  7. require 'View.php';
  8. //添加服务容器层
  9. class Container{
  10. // 容器属性, 就是一个数组,里面全是创建对象的方法
  11. protected $instance = [];
  12. // 1. 放进去: 将类的实例化过程绑定到容器中 $alias: 类实例的别名,
  13. public function bind($alias, \Closure $process){
  14. // 将类实例化的方法绑定/ 存储到服务容器中
  15. $this->instance[$alias] = $process;
  16. }
  17. // 2. 取出来: 执行容器中的实例方法
  18. public function make($alias, $params=[]){
  19. return call_user_func_array($this->instance[$alias], []);
  20. }
  21. }
  22. // 实例化容器
  23. $container = new Container();
  24. // 用到模型对象, 视图对象,将它们绑定到容器中
  25. $container->bind('model', function () {return new Model();});
  26. $container->bind('view', function () {return new View();});
  27. // Facade技术: 规范/统一了对外部对象的调用方式, 全部改为了静态调用, 不管之前的方法是什么类型
  28. // 添加Facade门面类
  29. class Facade{
  30. protected static $container = null;
  31. protected static $data = [];
  32. // 用服务容器给它初始化
  33. public static function initialize(Container $container){
  34. static::$container = $container;
  35. }
  36. // 用静态代理方式将模型中的getData()静态化
  37. public static function getData(){
  38. static::$data = static::$container->make('model')->getData();
  39. }
  40. // 用静态代理方式将视图中的fetch()静态化
  41. public static function fetch(){
  42. return static::$container->make('view')->fetch(static::$data);
  43. }
  44. }
  45. class Student extends Facade{
  46. //...
  47. }
  48. // 3. 创建控制器
  49. class Controller{
  50. public function __construct(Container $container){
  51. // 调用Facade里面的初始化方法
  52. Student::initialize($container);
  53. }
  54. public function index(){
  55. // 3.1 获取数据
  56. Student::getData();
  57. // 3.2 渲染模板
  58. return Student::fetch();
  59. }
  60. }
  61. // 4. 客户端调用/访问类成员 将模型对象与视图对象,以参数的方式再次注入到控制器的方法
  62. $controller = new Controller($container);
  63. echo $controller->index();

总结

感觉mvc模式挺简单的,或许是我了解的还不够深入吧。看来还是需要更进一步的加强学习。

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