Blogger Information
Blog 13
fans 1
comment 0
visits 8260
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0513MVC-简单控制
扬美刘
Original
757 people have browsed it

MVC的基本知识

MVC即模型model,视图view,控制器controller
一般地model用来处理与数据库的联系,
view用来渲染用户端界面,
controller用来控制数据流程,即就是对接model和view
model <-> controller <-> view

简单结构的MVC例子

  • 1) 创建模型model,生成数据$data(),存为文件model.php

    1. namespace mvcdemo;
    2. // 链接数据库;
    3. require ('pdoconnet.php');
    4. // 建立模型类: 用于数据库操作
    5. class Model
    6. {
    7. public function getData()
    8. { global $pdo;
    9. return $pdo->query('SELECT * FROM `T_user` LIMIT 10')->fetchAll();
    10. }
    11. }
    12. // print_r ((new Model)->getData());
  • 2)创建视图,就是用户端的界面样式,生成view.php文件

    1. namespace mvcdemo;
    2. class View{
    3. public function fecth($data)
    4. {
    5. $table='<table>';
    6. $table.='<tr><th>ID</th><th>姓名</th><th>性别</th><th>手机号</th></tr>';
    7. foreach ($data as $user){
    8. $table.= '<tr><th>'.$user['ID'].'</th>';
    9. $table.= '<th>'.$user['username'].'</th>';
    10. $table.= '<th>'.($user['sex']?'男':'女').'</th>';
    11. $table.= '<th>'.$user['mtel'].'</th></tr>';
    12. }
    13. $table.= '</table>';
    14. return $table;
    15. }
    16. }
    17. // 获得数据
    18. // require ('model.php') ;
    19. // $arr=(new Model)->getData();
    20. // 打印视图
    21. // echo (new View)->fecth($arr);
  • 3)创建控制器,生成controller.php文件

    1. namespace mvcdemo;
    2. // 导入两个文件
    3. require ('model.php');
    4. require ('view.php');
    5. class controller
    6. {
    7. public function getindex()
    8. {
    9. // 取得数据
    10. $Model=new Model;
    11. $data=$Model->getData();
    12. // 取得视图
    13. $View=new View;
    14. return $View->fecth($data);
    15. }
    16. }
    17. // 渲染结果
    18. $controller=new controller;
    19. echo $controller->getindex();
    20. echo (new controller)->getindex();
  • 4) 在controller.php显示的结果

注入点的优化

由于现有控制器的数据注入点是在函数中写错,对于引用不方便,因此可以难过修改注入点,让程序变得更灵活,这就是注入的选择

  • )通过函数传参数来注入

    1. namespace mvcdemo;
    2. require ('model.php');
    3. require ('view.php');
    4. class controller2
    5. {
    6. public function getindex(Model $model, View $view)
    7. {
    8. // 取得数据
    9. $data=$model->getData();
    10. // 取得视图
    11. return $view->fecth($data);
    12. }
    13. }
    14. // 渲染结果,通过函数传参,把二个new通过参数传入
    15. $model=new Model;
    16. $view=new View;
    17. $controller=new controller2;
    18. echo $controller->getindex($model,$view);
  • 通过构造法传入

  1. class controller3
  2. {
  3. private $model;
  4. private $view;
  5. public function __construct(Model $model,View $view)
  6. {
  7. $this->model=$model;
  8. $this->view=$view;
  9. }
  10. public function getindex()
  11. {
  12. // 取得数据
  13. $data=$this->model->getData();
  14. // 取得视图
  15. return $this->view->fecth($data);
  16. }
  17. }
  18. // 渲染结果
  19. $model=new Model;
  20. $view=new View;
  21. $controller=new controller3($model,$view);
  22. echo $controller->getindex();
  23. ?>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:mvc的流程和思想是很简单的
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