Blogger Information
Blog 38
fans 0
comment 0
visits 18537
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
MVC课后练习
Blackeye
Original
425 people have browsed it

一、core

  1. Model.php
  2. View.php
  3. Controller.php
  1. <?php
  2. namespace phpedu;
  3. use PDO;
  4. class Model{
  5. protected $db;
  6. // 控制LIMIT参数
  7. protected $n;
  8. public function __construct($dsn,$username,$password,$n=10){
  9. $this->db = new PDO($dsn,$username,$password);
  10. $this->n = $n;
  11. }
  12. // 获取数据库数据并返回
  13. public function getAll(){
  14. $stmt = $this->db->prepare('SELECT * FROM `staff` LIMIT ?');
  15. $stmt -> bindParam(1,$this->n,PDO::PARAM_INT);
  16. $stmt->execute();
  17. return $stmt->fetchALL();
  18. }
  19. }
  1. <?php
  2. namespace phpedu;
  3. class View{
  4. public function display($data){
  5. // 把数据赋给模板变量
  6. $staffs=$data;
  7. //引入模板
  8. require ROOT_PATH . '\\' . 'view\show.php';
  9. }
  10. }
  1. <?php
  2. namespace phpedu;
  3. // 控制器类似于中间人代理model以及view
  4. class Controller{
  5. protected $model;
  6. protected $view;
  7. // 初始化模型和视图对象
  8. public function __construct($model, $view){
  9. $this->model = $model;
  10. $this->view = $view;
  11. }
  12. // 页面渲染
  13. public function index(){
  14. $data = $this->model->getAll();
  15. $this->view->display($data);
  16. }
  17. }

二、Model、View、Controller

  1. <?php
  2. namespace phpedu;
  3. //自定义模板类
  4. class StaffModel extends Model{
  5. }
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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 border="1" cellspacing="0" cellpadding="5" width="400">
  11. <caption style="font-size: 1.2em;">员工信息表</caption>
  12. <thead bgcolor="lightcyan">
  13. <tr>
  14. <th>id</th>
  15. <th>姓名</th>
  16. <th>性别</th>
  17. <th>邮箱</th>
  18. <th>操作</th>
  19. </tr>
  20. </thead>
  21. <tbody align="center">
  22. <?php foreach ($staffs as [$id, $name, $sex, $email]) : ?>
  23. <tr>
  24. <td><?= $id ?></td>
  25. <td><?= $name ?></td>
  26. <td><?= $sex ? '女' : '男' ?></td>
  27. <td><?= $email ?></td>
  28. <td>
  29. <a href="">编辑</a>
  30. <a href="">删除</a>
  31. </td>
  32. </tr>
  33. <?php endforeach ?>
  34. </tbody>
  35. </table>
  36. </body>
  37. </html>
  1. <?php
  2. namespace phpedu;
  3. // 自定义控制器
  4. class IndexController extends Controller{
  5. }

三、配置,入口文件:config.php、index.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' => 'root'
  11. ]);
  12. // 应用相关
  13. define('APP', [
  14. // 默认控制器
  15. 'default_controller' => 'index',
  16. // 默认方法
  17. 'default_action' => 'index'
  18. ]);
  19. // 项目根路径
  20. define('ROOT_PATH', __DIR__);
  1. <?php
  2. namespace phpedu;
  3. // 加载配置项
  4. require __DIR__ . '/config.php';
  5. // 加载框架的核心类
  6. require __DIR__ . '/core/Model.php';
  7. require __DIR__ . '/core/View.php';
  8. require __DIR__ . '/core/Controller.php';
  9. // 加载自定义模型
  10. require __DIR__ . '/model/StaffModel.php';
  11. // 获取方法及控制器参数
  12. $c = $_GET['c'] ?? APP['default_controller'];
  13. $a = $_GET['a'] ?? APP['default_action'];
  14. $class = ucfirst($c) . 'Controller';
  15. require __DIR__ . '/controller/' . $class . '.php';
  16. // 获取模型对象
  17. extract(DATABASE);
  18. $dsn = sprintf("%s:dbname=%s", $type, $dbname);
  19. $model = new StaffModel($dsn, $username, $password, 5);
  20. // 获取视图对象
  21. $view = new View();
  22. // 获取控制器对象
  23. $realclass = __NAMESPACE__ . '\\' . $class;
  24. $controller = new $realclass($model, $view);
  25. echo $controller->$a();
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!