Blogger Information
Blog 42
fans 5
comment 0
visits 38192
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1207作业:简易mvc框架 电影网
张浩刚
Original
1119 people have browsed it

MVC框架

先上图




客户端与控制器部分

控制器:controller.php

  1. <?php
  2. //服务容器层
  3. namespace controller;
  4. use Closure;
  5. use config\model;
  6. use view\header;
  7. use view\index;
  8. use view\listinfo;
  9. use view\article;
  10. use view\footer;
  11. // require __DIR__ . '/atuoload.php';
  12. require __DIR__ . '/config/model.php';
  13. require __DIR__ . '/view/index.php';
  14. require __DIR__ . '/view/header.php';
  15. require __DIR__ . '/view/listinfo.php';
  16. require __DIR__ . '/view/article.php';
  17. require __DIR__ . '/view/footer.php';
  18. // 服务容器类
  19. class container{
  20. //容器属性时一个空数组,里面装创建对象的方法
  21. protected $instance = [];
  22. // 放进去,把实例化过程绑定到容器中
  23. // $alias 类实类的别名 可以自定义;Closure用于代表 匿名函数 的类
  24. public function bind($alias, Closure $process)
  25. {
  26. //将类实例化的方法 存储到容器中
  27. $this->instance[$alias] = $process;
  28. }
  29. //取出来,执行容器中的实例方法
  30. public function make($alias, $arr = [])
  31. {
  32. //call_user_func_array 回调函数,第一个是回调函数,第二个时索引数组
  33. //如call_user_func_array($a, $b) 把($a)作为回调函数调用,把参数数组作($b)为回调函数的参数传入。
  34. return call_user_func_array($this->instance[$alias], $arr);
  35. }
  36. }
  37. $container = new container();
  38. $container->bind( 'model', function(){return new model();} );
  39. $container->bind('header', function(){return new header();});
  40. $container->bind('index', function(){return new index();});
  41. $container->bind('listinfo', function(){return new listinfo();});
  42. $container->bind('article', function(){return new article();});
  43. $container->bind('footer', function(){return new footer();});
  44. // facade门面类
  45. class Facade{
  46. private static $container = [];
  47. private static $data = [];
  48. // 导入容器,并静态化
  49. public static function initialize(container $container){
  50. static::$container = $container;
  51. }
  52. // 导出model,并静态化
  53. public static function model(){
  54. static::$data = static::$container->make('model')->getData();
  55. }
  56. // 导出视图index层,并静态化
  57. public static function index(){
  58. return static::$container->make('index')->index(static::$data);
  59. }
  60. // 导出视图header层,并静态化
  61. public static function header(){
  62. return static::$container->make('header')->head(static::$data);
  63. }
  64. // 导出视图list层,并静态化
  65. public static function listinfo(){
  66. return static::$container->make('listinfo')->listinfo(static::$data);
  67. }
  68. // 导出视图article层,并静态化
  69. public static function article(){
  70. return static::$container->make('article')->article(static::$data);
  71. }
  72. // 导出视图footer层,并静态化
  73. public static function footer(){
  74. return static::$container->make('footer')->footer();
  75. }
  76. }
  77. //控制器
  78. class demo{
  79. public function __construct(container $container)
  80. {
  81. Facade::initialize($container);
  82. }
  83. public function index(){
  84. //导入数据
  85. Facade::model();
  86. //渲染首页模板
  87. Facade::header();
  88. Facade::index();
  89. Facade::footer();
  90. }
  91. public function list(){
  92. //导入数据
  93. Facade::model();
  94. //渲染列表页模板
  95. Facade::header();
  96. Facade::listinfo();
  97. Facade::footer();
  98. }
  99. public function article(){
  100. //导入数据
  101. Facade::model();
  102. //渲染内容页模板
  103. Facade::header();
  104. Facade::article();
  105. Facade::footer();
  106. }
  107. }
  108. $demo = new demo($container);

客户端首页:index.php

  1. <?php
  2. require __DIR__ . '/controller.php';
  3. //客户端 首页
  4. echo $demo->index();

客户端列表页:list.php

注意:在模板中使用了 list.php?id= 请注意命名
  1. <?php
  2. require __DIR__ . '/controller.php';
  3. //客户端 列表页
  4. echo $demo->list();

客户端 内容页:article.php

注意:在模板中使用了 article.php?id= 请注意命名
  1. <?php
  2. require __DIR__ . '/controller.php';
  3. //客户端 内容页
  4. echo $demo->article();

数据库model.php

  1. <?php
  2. namespace config;
  3. use PDO;
  4. class model{
  5. //pdo
  6. private static $pdo;
  7. //数据库表
  8. private static $table = null;
  9. public function __construct($dns='mysql:host=localhost;dbname=film',$user='root',$password='root')
  10. {
  11. static::$pdo = new PDO($dns,$user,$password);
  12. }
  13. //获取电影栏目表
  14. private static function listinfo($table='listinfo'){
  15. static::$table = $table;
  16. $sql = 'SELECT * FROM ' . static::$table;
  17. $stmt = static::$pdo->prepare($sql);
  18. $stmt->execute();
  19. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  20. }
  21. //获取电影内容表
  22. private static function article($table='article'){
  23. static::$table = $table;
  24. $sql = 'SELECT * FROM ' . static::$table;
  25. $stmt = static::$pdo->prepare($sql);
  26. $stmt->execute();
  27. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  28. }
  29. //获取会员表
  30. private static function user($table='user'){
  31. static::$table = $table;
  32. $sql = 'SELECT * FROM ' . static::$table . ' LIMIT 0,3';
  33. $stmt = static::$pdo->prepare($sql);
  34. $stmt->execute();
  35. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  36. }
  37. //将电影栏目和内容详情页,会员数据包整合到一个类中
  38. public function getData(){
  39. return [
  40. 'article'=>static::article(),
  41. 'list'=>static::listinfo(),
  42. 'user'=>static::user()
  43. ];
  44. }
  45. }

1. 注意栏目主键与内容页索引关联


视图模板层

1.$vv[‘uid’] == $v[‘id’] 才能对应调用

index.php

  1. <?php
  2. namespace view;
  3. class index{
  4. public function index($data){
  5. foreach ($data['list'] as $v){
  6. echo '<h2><a href="list.php?id=' . $v['id'] . '">' . $v['title'] . '</a></h2>';
  7. echo '<ul>';
  8. foreach($data['article'] as $vv){
  9. if($vv['uid'] == $v['id']){
  10. echo '<li><a href="article.php?id='. $vv['id'] .'">'.$vv['title'].'</a></li>';
  11. }
  12. }
  13. echo '</ul>';
  14. }
  15. }
  16. }
  17. ?>

header.php

  1. <?php
  2. namespace view;
  3. class header{
  4. public function head($data)
  5. {
  6. echo '<header>';
  7. echo '<nav>';
  8. echo '<a href="/">首页</a>';
  9. foreach($data['list'] as $v){
  10. echo "<a href=list.php?id={$v['id']}>{$v['title']}</a>";
  11. }
  12. echo '</nav>';
  13. echo '</header>';
  14. }
  15. }
  16. echo '
  17. <style>
  18. a{text-decoration: none;color:#333;}
  19. header{width: 100%; height: 60px;background-color: lightcoral;}
  20. header>nav{height: 100%;display:grid;grid-template-columns: repeat(4,1fr);place-items: center;}
  21. header>nav>a{color:white;text-decoration: none;font-size: 20px;}
  22. footer{width: 100%; height: 60px;background-color: lightcoral;display: flex;justify-content: center;align-items: center;color: white;}
  23. </style>';
  24. ?>

listinfo.php

通过$_GET对应调用
  1. <?php
  2. namespace view;
  3. class listinfo{
  4. public function listinfo($data){
  5. foreach ($data['list'] as $v) {
  6. if($_GET['id'] == $v['id']){
  7. echo '<h2><a href="list.php?id=' . $_GET['id'] . '">' . $v['title'] . '</a></h2>';
  8. }
  9. }
  10. echo '<ul>';
  11. foreach ($data['article'] as $v) {
  12. if($_GET['id'] == $v['uid']){
  13. echo '<li><a href=article.php?id='.$v['id'].'>' . $v['title'] . '</a></li>';
  14. }
  15. }
  16. echo '</ul>';
  17. }
  18. }
  19. ?>

article.php

  1. <?php
  2. namespace view;
  3. class article{
  4. public function article($data){
  5. foreach ($data['article'] as $v) {
  6. if($_GET['id'] == $v['id']){
  7. echo '<h2>' . $v['title'] . '</h2>';
  8. echo '<img src=' . $v['img'] . '>';
  9. echo '<p>' . $v['newstext'] . '<p>';
  10. }
  11. }
  12. }
  13. }
  14. ?>
  1. <?php
  2. namespace view;
  3. class footer{
  4. public function footer(){
  5. echo '<footer><p>php中文网</p></footer>';
  6. }
  7. }

总结:

1、完成了一个傻瓜式mvc,真的傻瓜式。
2、php相关知识串联学习,收获颇丰
3、来不及做会员登陆了,但已经有思路了

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!