Blogger Information
Blog 43
fans 4
comment 0
visits 19267
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP之MVC底层实现
汇享科技
Original
616 people have browsed it

MVC框架

Model:模型类,数据库操作
View:视图类,展示到客户端
Controller:控制器,协调模型与视图

MVC目录架构:
36353-kh3zrcb12dr.png

  • 具体代码如下:

  • Model.php

  1. namespace core;
  2. use PDO;
  3. abstract class Model
  4. {
  5. protected $db;
  6. public function __construct($dsn,$username,$password)
  7. {
  8. $this->db = new PDO($dsn,$username,$password);
  9. }
  10. public function getAll($num)
  11. {
  12. $sql = "SELECT * FROM `bittel` LIMIT ?;";
  13. $stmt = $this->db->prepare($sql);
  14. $stmt->bindParam(1,$num,PDO::PARAM_INT);
  15. if($stmt->execute()){
  16. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  17. }else{
  18. print_r($stmt->errorInfo());
  19. print_r($stmt->debugDumpParams());
  20. }
  21. }
  22. }
  • View.php
  1. namespace core;
  2. class View
  3. {
  4. //1.创建模板变量
  5. protected array $data = [];
  6. //2.模板赋值
  7. public function assign(string $key, $value)
  8. {
  9. $this->data[$key] = $value;
  10. }
  11. //3.模板渲染
  12. public function render($path, $data = [])
  13. {
  14. //判断是否有数据传入
  15. if ($data) {
  16. foreach ($data as $key => $value) {
  17. $this->assign($key, $value);
  18. }
  19. }
  20. //数组解构
  21. extract($this->data);
  22. file_exists($path) ? include $path : die('模板文件不存在');
  23. }
  24. }
  • Controller.php
  1. namespace core;
  2. abstract class Controller
  3. {
  4. //模型对象
  5. protected Model $model;
  6. //视图对象
  7. protected View $view;
  8. public function __construct($model,$view)
  9. {
  10. //初始化两个对象
  11. $this->view = $view;
  12. $this->model = $model;
  13. }
  14. }
  • Router
  1. namespace core;
  2. //路由类
  3. class Router
  4. {
  5. public static function parse(){
  6. // 默认控制器,实际项目,应该来自配置文件,而不是在写死
  7. $controller = 'User';
  8. $action = 'user';
  9. // 参数列表
  10. $params = [];
  11. // 判断是否存在pathinfo
  12. if (array_key_exists('PATH_INFO', $_SERVER) && $_SERVER['PATH_INFO'] !== '/') {
  13. // 为什么要判断 $_SERVER['PATH_INFO'] !== '/' ?
  14. // 因为: admin.php/ 时,$_SERVER['PATH_INFO'] = '/', 导致解析控制器失败
  15. $pathinfo = array_filter(explode('/', $_SERVER['PATH_INFO']));
  16. // dump($pathinfo);
  17. // 考虑到index.php/ 情况, 这时pathinfo为空数组
  18. if (count($pathinfo) >= 2) {
  19. $controller = array_shift($pathinfo);
  20. $action = array_shift($pathinfo);
  21. $params = $pathinfo;
  22. } else {
  23. $controller = array_shift($pathinfo);
  24. }
  25. }
  26. // 查看控制器,方法,参数
  27. // dump($controller, $action, $params);
  28. // 将这些数据返回出去
  29. return [$controller, $action, $params];
  30. }
  31. }
  • 自定义模型 UserModel.php
  1. use core\Model;
  2. class UserModel extends Model
  3. {
  4. public function __construct($dsn, $username, $password)
  5. {
  6. parent::__construct($dsn, $username, $password);
  7. }
  8. }
  • 自定义控制器 UserController.php
  1. use core\Model;
  2. class UserModel extends Model
  3. {
  4. public function __construct($dsn, $username, $password)
  5. {
  6. parent::__construct($dsn, $username, $password);
  7. }
  8. }
  • view文件 user.php
  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. <style>
  9. table {
  10. margin: auto;
  11. width: 600px;
  12. border-collapse: collapse;
  13. text-align: center;
  14. }
  15. table th,
  16. table td {
  17. border: 1px solid;
  18. padding: 5px;
  19. }
  20. table thead {
  21. background-color: lightcyan;
  22. }
  23. table caption {
  24. font-size: larger;
  25. margin-bottom: 8px;
  26. }
  27. body>p {
  28. display: flex;
  29. }
  30. p>a {
  31. text-decoration: none;
  32. color: #555;
  33. border: 1px solid;
  34. padding: 5px 10px;
  35. margin: 10px 2px;
  36. }
  37. .active {
  38. background-color: seagreen;
  39. color: white;
  40. border: 1px solid seagreen;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <table>
  46. <caption><h3>用户信息表</h3></caption>
  47. <thead>
  48. <tr>
  49. <th>ID</th>
  50. <th>姓名</th>
  51. <th>性别</th>
  52. <th>邮箱</th>
  53. </tr>
  54. </thead>
  55. <tbody>
  56. <?php foreach ($users as $user) :extract($user) ?>
  57. <tr>
  58. <td><?=$id?>
  59. </td>
  60. <td><?=$name?>
  61. </td>
  62. <td><?=$sex?'女':'男' ?>
  63. </td>
  64. <td><?=$email?>
  65. </td>
  66. </tr>
  67. <?php endforeach ?>
  68. </tbody>
  69. </table>
  70. </body>
  71. </html>
  • 入口文件 1.index.php
  1. namespace Mvc;
  2. use controller\UserController;
  3. use model\UserModel;
  4. use core\View;
  5. // 入口文件: 测试
  6. // 1.类的自动 composer的自动加载器
  7. require 'vendor/autoload.php';
  8. // 2. 实例化控制器
  9. $model = new UserModel('mysql:dbname=bittel', 'root', 'root');
  10. //print_r($model);
  11. //echo $model;
  12. $view = new View();
  13. $users = new UserController($model, $view);
  14. // 3. 调用控制器中的方法
  15. $users->getDate(10);
  • 入口文件 2. index1.php
  1. namespace Mvc;
  2. use core\Router;
  3. use core\View;
  4. use Model\UserModel;
  5. require 'vendor/autoload.php';
  6. require 'model/UserModel.php';
  7. $request = Router::parse();
  8. $controller = array_shift($request);
  9. $method = array_shift($request);
  10. $params = array_shift($request);
  11. // 生成控制器名称
  12. $controller= 'controller\\'. ucfirst($controller). 'Controller';
  13. //echo $controller;
  14. //die();
  15. $model = new UserModel('mysql:dbname=bittel', 'root', 'root');
  16. //print_r($model);
  17. //echo $model;
  18. $view = new View();
  19. $users = new $controller($model, $view);
  20. call_user_func_array([$users, 'getDate'], $params);
  • 效果如下:

03197-07tprr62upln.png

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!