批改状态:合格
老师批语:
依照课堂案例,试着自己写一个迷你的MVC小框架,流程走通即可,并写出详细步骤和心得体会
修改数据库 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/medoocomposer search plates 搜索结果找到league/plates Plates, the native PHP template system that’s fast, easy to use and easy to extend.
composer require league/plates
// 模型namespace core;// Using Medoo namespaceuse Medoo\Medoo;class Model extends Medoo{public function __construct(){parent::__construct(// required['database_type' => 'mysql','database_name' => 'phpedu','server' => 'localhost','username' => 'root','password' => 'root']);}}
// 视图namespace core;// Using League\Plates namespaceuse League\Plates\Engine;class View extends Engine{public function __construct($path){parent::__construct($path);}}
// 员工模型namespace models;// Using core namespaceuse core\Model;// 员工模型class StaffsModel extends Model{public function __construct(){parent::__construct();}}
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>员工信息</title><style>body {display: flex;flex-direction: column;align-items: center;}table {border-collapse: collapse;border: 1px solid;width: 50%;text-align: center;}th,td {border: 1px solid;padding: 5px;}tr:first-child {background-color: #eee;}</style></head><body><h3>用户管理系统</h3><table><tr><th>id</th><th>姓名</th><th>性别</th><th>工资</th><th>邮箱</th><th>操作</th></tr><?php foreach ($staffs as $staff) : ?><tr><td><?= $this->e($staff['sid']) ?></td><td><?= $this->e($staff['name']) ?></td><td><?= $this->e($staff['gender']) == 'male' ? '男' : '女' ?></td><td><?= $this->e($staff['salary']) ?></td><td><?= $this->e($staff['email']) ?></td><td><button>编辑</button><button>删除</button></td></tr><?php endforeach ?></table><p><a href="">1</a><a href="">2</a><a href="">3</a><a href="">4</a><a href="">5</a><a href="">6</a></p></body></html>
namespace controllers;class StaffsController{public $model;public $view;public function __construct($model, $view){$this->model = $model;$this->view = $view;}public function index() {// 获取数据// https://medoo.lvtao.net/1.2/doc.select.php$staffs = $this->model->select('staffs', ['sid', 'name', 'gender', 'salary', 'email'], ['salary[>=]' => 8000, 'LIMIT' => 8]);// 模板赋值// https://platesphp.com/getting-started/simple-example/return $this->view->render('staffs/list', ['staffs' => $staffs]);}}
{"name": "0304-frame/test.com","description": "0304-frame test","autoload": {"psr-4": {"controllers\\": "app/controllers","models\\": "app/models","core\\": "core"}},"require": {"catfan/medoo": "^1.7","league/plates": "^3.4"}}
composer dump 更新自动加载
// 自动加载require __DIR__ .'/vendor/autoload.php';use controllers\StaffsController;use models\StaffsModel;use core\View;// 测试模型$model = new StaffsModel();// 测试视图$view = new View('app/views');// 测试控制器$controller = new StaffsController($model, $view);echo $controller->index();

core/ 核心目录,文件,接管组件功能app/ 应用程序目录,分类管理models 管理模型类,和拓展私有功能,扩展组件功能controllers 控制器,集中处理渲染模板的业务逻辑views 视图,模板渲染数据,修改控制器,渲染不同模板
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号