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

MVC 实例演示

实现效果

实现效果

自定义控制器和方法

自定义控制器和方法

配置文件 config.php

  1. <?php
  2. //配偶文件
  3. //数据库配置信息
  4. const DATABASE = [
  5. 'type' => 'mysql',
  6. 'dbname' => 'phpedu',
  7. 'username' => 'root',
  8. 'password' => 'root'
  9. ];
  10. //根目录
  11. const ROOT_PATH = __DIR__;
  12. //应用相关
  13. const APP = [
  14. //默认控制器
  15. 'default_controller' => 'index',
  16. //控制器默认使用的方法
  17. 'default_action' => 'index',
  18. ];

核心类库

1. 模型类 Model.php

  1. <?php
  2. //核心类库
  3. namespace phpcn;
  4. use PDO;
  5. class Model
  6. {
  7. //创建链接对象
  8. protected $db;
  9. //Model对象实例化时,链接数据库
  10. public function __construct($dsn, $username, $password)
  11. {
  12. $this->db = new PDO($dsn, $username, $password);
  13. }
  14. //公共方法供用户进行数据库的操作
  15. public function getAll($n = 5)
  16. {
  17. $stmt = $this->db->prepare("SELECT * FROM `staff` LIMIT $n");
  18. $stmt->execute();
  19. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  20. }
  21. }

2.1 视图类 View.php

  1. <?php
  2. //核心类库
  3. namespace phpcn;
  4. class View
  5. {
  6. //视图类实现模板的赋值和模板的渲染
  7. public function display($staffs)
  8. {
  9. include ROOT_PATH . '/view/' . 'show.php';
  10. }
  11. }

2.2 模板 show.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>员工信息表</title>
  8. </head>
  9. <body>
  10. <table>
  11. <caption>员工信息表</caption>
  12. <thead>
  13. <tr>
  14. <th>id</th>
  15. <th>姓名</th>
  16. <th>性别</th>
  17. <th>邮箱</th>
  18. </tr>
  19. </thead>
  20. <tbody>
  21. <?php foreach ($staffs as $staff) : ?>
  22. <?php extract($staff) ?>
  23. <tr>
  24. <td><?= $id ?></td>
  25. <td><?= $name ?> </td>
  26. <td><?= $gender ? "男" : "女" ?> </td>
  27. <td><?= $email ?> </td>
  28. </tr>
  29. <?php endforeach ?>
  30. </tbody>
  31. </table>
  32. </body>
  33. </html>

3.控制器类 Controller.php

  1. <?php
  2. //核心类库
  3. namespace phpcn;
  4. class Controller
  5. {
  6. //模型对象
  7. protected $model;
  8. //视图对象
  9. protected $view;
  10. //控制器实例化时要确保模型与对象可用
  11. public function __construct($model, $view)
  12. {
  13. $this->model = $model;
  14. $this->view = $view;
  15. }
  16. public function index($n = 5)
  17. {
  18. //通过模型中的公共方法获取数据
  19. $staffs = $this->model->getAll($n);
  20. //将数据赋值到模板中,通过视图类渲染模板
  21. $this->view->display($staffs);
  22. }
  23. }

4.自定义控制器类 UserController.php

  1. <?php
  2. namespace phpcn;
  3. class UserController extends Controller
  4. {
  5. public function user()
  6. {
  7. echo '<h2>Hello PHPcn</h2>';
  8. }
  9. }

5.入口文件 index.php

  1. <?php
  2. //入口文件
  3. namespace phpcn;
  4. //加载配置项
  5. require __DIR__ . '/config.php';
  6. //加载核心类库
  7. require ROOT_PATH . '/core/Model.php';
  8. require ROOT_PATH . '/core/View.php';
  9. require ROOT_PATH . '/core/Controller.php';
  10. //加载自定义模型
  11. require ROOT_PATH . '/model/StaffModel.php';
  12. //结构数据库参数数组
  13. extract(DATABASE);
  14. //实例化模型类
  15. $dsn = sprintf('%s:dbname=%s', $type, $dbname);
  16. $model = new StaffModel($dsn, $username, $password);
  17. //实例化视图类
  18. $view = new View(null, null);
  19. //获取自定义控制器和自定义方法
  20. $c = $_GET['c'] ?? APP['default_controller'];
  21. $a = $_GET['a'] ?? APP['default_action'];
  22. //获取定义控制器名
  23. $class = ucfirst($c) . 'Controller';
  24. //加载自定义控制器
  25. require ROOT_PATH . '/Controller/' . $class . '.php';
  26. //完整控制器类名
  27. $fullclass = __NAMESPACE__ . '\\' . $class;
  28. $controller = new $fullclass($model, $view);
  29. $controller->$a(10);
  30. //执行自定义控制器和方法
  31. //index.php?c=user&a=user
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