PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > 迷你MVC小框架-走通流程步骤-心得体会

迷你MVC小框架-走通流程步骤-心得体会

葡萄枝子
葡萄枝子 原创
2021年03月05日 00:40:34 636浏览

迷你MVC小框架-走通流程步骤-心得体会

依照课堂案例,试着自己写一个迷你的MVC小框架,流程走通即可,并写出详细步骤和心得体会

1. medoo 数据库框架,plates 原生PHP模板系统

1.1 加载组件 medoo 数据库框架

  • 修改数据库 id 字段为 sid alter table staffs change id sid int unsigned auto_increment

  • vscode 打开终端进入创建的 0304-frame 目录

  • 终端 composer search medoo 搜索结果找到

catfan/medoo The lightweight PHP database framework to accelerate development

  • 终端 composer require catfan/medoo

1.2 加载组件 plates 模板系统框架

  • 终端 composer search plates 搜索结果找到

league/plates Plates, the native PHP template system that’s fast, easy to use and easy to extend.

  • 终端 composer require league/plates

加载组件

2. 创建核心目录 core

  1. // 模型
  2. namespace core;
  3. // Using Medoo namespace
  4. use Medoo\Medoo;
  5. class Model extends Medoo
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct(
  10. // required
  11. [
  12. 'database_type' => 'mysql',
  13. 'database_name' => 'phpedu',
  14. 'server' => 'localhost',
  15. 'username' => 'root',
  16. 'password' => 'root'
  17. ]
  18. );
  19. }
  20. }
  1. // 视图
  2. namespace core;
  3. // Using League\Plates namespace
  4. use League\Plates\Engine;
  5. class View extends Engine
  6. {
  7. public function __construct($path)
  8. {
  9. parent::__construct($path);
  10. }
  11. }

3. 创建应用目录 app,app\models,app\views,app\controllers

  • app/models/StaffsModel.php
  1. // 员工模型
  2. namespace models;
  3. // Using core namespace
  4. use core\Model;
  5. // 员工模型
  6. class StaffsModel extends Model
  7. {
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. }
  12. }
  • app/views/staffs/list.php
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>员工信息</title>
  7. <style>
  8. body {
  9. display: flex;
  10. flex-direction: column;
  11. align-items: center;
  12. }
  13. table {
  14. border-collapse: collapse;
  15. border: 1px solid;
  16. width: 50%;
  17. text-align: center;
  18. }
  19. th,
  20. td {
  21. border: 1px solid;
  22. padding: 5px;
  23. }
  24. tr:first-child {
  25. background-color: #eee;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <h3>用户管理系统</h3>
  31. <table>
  32. <tr>
  33. <th>id</th>
  34. <th>姓名</th>
  35. <th>性别</th>
  36. <th>工资</th>
  37. <th>邮箱</th>
  38. <th>操作</th>
  39. </tr>
  40. <?php foreach ($staffs as $staff) : ?>
  41. <tr>
  42. <td><?= $this->e($staff['sid']) ?></td>
  43. <td><?= $this->e($staff['name']) ?></td>
  44. <td><?= $this->e($staff['gender']) == 'male' ? '男' : '女' ?></td>
  45. <td><?= $this->e($staff['salary']) ?></td>
  46. <td><?= $this->e($staff['email']) ?></td>
  47. <td><button>编辑</button><button>删除</button></td>
  48. </tr>
  49. <?php endforeach ?>
  50. </table>
  51. <p>
  52. <a href="">1</a>
  53. <a href="">2</a>
  54. <a href="">3</a>
  55. <a href="">4</a>
  56. <a href="">5</a>
  57. <a href="">6</a>
  58. </p>
  59. </body>
  60. </html>
  • app/views/controllers/StaffsController.php
  1. namespace controllers;
  2. class StaffsController
  3. {
  4. public $model;
  5. public $view;
  6. public function __construct($model, $view)
  7. {
  8. $this->model = $model;
  9. $this->view = $view;
  10. }
  11. public function index() {
  12. // 获取数据
  13. // https://medoo.lvtao.net/1.2/doc.select.php
  14. $staffs = $this->model->select('staffs', ['sid', 'name', 'gender', 'salary', 'email'], ['salary[>=]' => 8000, 'LIMIT' => 8]);
  15. // 模板赋值
  16. // https://platesphp.com/getting-started/simple-example/
  17. return $this->view->render('staffs/list', ['staffs' => $staffs]);
  18. }
  19. }

4. 修改 composer.json 将空间映射到目录

  • 添加自动加载配置项
  1. {
  2. "name": "0304-frame/test.com",
  3. "description": "0304-frame test",
  4. "autoload": {
  5. "psr-4": {
  6. "controllers\\": "app/controllers",
  7. "models\\": "app/models",
  8. "core\\": "core"
  9. }
  10. },
  11. "require": {
  12. "catfan/medoo": "^1.7",
  13. "league/plates": "^3.4"
  14. }
  15. }
  • 终端运行 composer dump 更新自动加载

更新加载

5. 入口文件 index.php

  1. // 自动加载
  2. require __DIR__ .'/vendor/autoload.php';
  3. use controllers\StaffsController;
  4. use models\StaffsModel;
  5. use core\View;
  6. // 测试模型
  7. $model = new StaffsModel();
  8. // 测试视图
  9. $view = new View('app/views');
  10. // 测试控制器
  11. $controller = new StaffsController($model, $view);
  12. echo $controller->index();

运行结果

6. 心得体会

  • core/ 核心目录,文件,接管组件功能
  • app/ 应用程序目录,分类管理
    • models 管理模型类,和拓展私有功能,扩展组件功能
    • controllers 控制器,集中处理渲染模板的业务逻辑
    • views 视图,模板渲染数据,修改控制器,渲染不同模板
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议