Blogger Information
Blog 33
fans 0
comment 0
visits 18840
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
MVC实例演示
李玉峰
Original
689 people have browsed it

1.框架的入口文件:index.php

  1. <?php
  2. namespace phpcn;
  3. // 框架的入口文件
  4. //1.加载配置项
  5. require __DIR__ . '/config.php';
  6. //2.加载框架核心类库
  7. require __DIR__ . '/core/Controller.php';
  8. require __DIR__ . '/core/Model.php';
  9. require __DIR__ . '/core/View.php';
  10. //3.加载自定义模型
  11. require __DIR__ . '/model/StaffModel.php';
  12. //4.实例化模型类
  13. //extract() :解构,将一个关联数组结构为一个个独立变量
  14. extract(DATABASE);
  15. //查看解构变量是否成功
  16. // echo $type;
  17. $dsn = sprintf('%s:dbname=%s',$type,$dbname);
  18. $model = new StaffMdel($dsn,$username,$password);
  19. //https://php.edu/0506/mvc3/index.php?c=controller&a=action
  20. //c = 控制器 a = action
  21. $c = $_GET['c'] ?? APP['default_controller'];
  22. $a = $_GET['a'] ?? APP['default_action'];
  23. //获取类名
  24. $class = ucfirst($c) . 'Controller';
  25. //加载自定义类
  26. require __DIR__ . '/controller/'. $class . '.php';
  27. //5.加载视图类
  28. $view = new View();
  29. //6.实例化控制器类
  30. $fullclass = __NAMESPACE__ . '\\' . $class;
  31. $controller = new $fullclass($model, $view);
  32. //执行控制器中index()方法
  33. echo $controller->$a();

2.配置项公共文件:config.php

  1. <?php
  2. //连接数据库参数
  3. define('DATABASE',[
  4. 'type'=>'mysql',
  5. 'host'=>'localhost',
  6. 'dbname'=>'phpedu',
  7. 'port'=>'3306',
  8. 'charset'=>'utf8',
  9. 'username'=>'root',
  10. 'password'=>'phpedu'
  11. ]);
  12. //应用相关
  13. define('APP',[
  14. //默认控制器
  15. 'default_controller'=>'index',
  16. //默认方法
  17. 'default_action'=>'index'
  18. ]);
  19. //根路径
  20. define('ROOT_PATH',__DIR__);

3.框架核心类库

3.1 控制器:Controller.php
  1. <?php
  2. namespace phpcn;
  3. class Controller
  4. {
  5. // 模型对象
  6. protected $model;
  7. // 视图对象
  8. protected $view;
  9. // 控制器类实例时,要确保模型和视图对象可用
  10. public function __construct($model, $view)
  11. {
  12. $this->model = $model;
  13. $this->view = $view;
  14. }
  15. public function index()
  16. {
  17. // 1. 模型: 获取数据
  18. $data = $this->model->getAll(10);
  19. // 2. 视图: 渲染模板
  20. $this->view->display($data);
  21. }
  22. }
3.2 模型:Model.php
  1. <?php
  2. namespace phpcn;
  3. use PDO;
  4. class Model
  5. {
  6. // 连接对象
  7. protected $db;
  8. // 模型实例化时,应该将数据库连接上,为后面的操作做好准备
  9. public function __construct($dsn, $username, $password)
  10. {
  11. $this->db = new PDO($dsn, $username, $password);
  12. }
  13. // 通常模型类中,会预置一些公共方法,供用户进行数据库操作
  14. // 获取分页数据(多条)
  15. public function getAll($n = 10)
  16. {
  17. $stmt = $this->db->prepare('SELECT * FROM `staff` LIMIT ?');
  18. // 默认绑定的都是字符串类型
  19. $stmt->bindParam(1, $n, PDO::PARAM_INT);
  20. $stmt->execute();
  21. // print_r($stmt->debugDumpParams());
  22. return $stmt->fetchAll();
  23. }
  24. }
  25. // 测试语句
  26. // print_r((new Model('mysql:dbname=phpedu', 'root', 'root'))->getAll());
3.3 视图:View.php
  1. <?php
  2. namespace phpcn;
  3. class View
  4. {
  5. public function display($data)
  6. {
  7. // 1. 模型赋值
  8. $staffs = $data;
  9. // 2. 渲染模型
  10. include ROOT_PATH . '/view/show.php';
  11. }
  12. }

4.模型:一张数据库表对应一个模型:StaffModel.php

  1. <?php
  2. namespace phpcn;
  3. class StaffMdel extends Model
  4. {
  5. //复用核心
  6. }

5.自动加载控制器:AdminController.php

例如:http://php.edu/0506/mvc3/index.php?c=admin&a=index

  1. <?php
  2. namespace phpcn;
  3. class AdminController extends Controller
  4. {
  5. public function index()
  6. {
  7. return '这里是后台';
  8. }
  9. }
Correcting teacher:PHPzPHPz

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