迷你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
- core/model.php 模型操作数据库 medoo.lvtao.net/1.2/doc.php
// 模型
namespace core;
// Using Medoo namespace
use Medoo\Medoo;
class Model extends Medoo
{
public function __construct()
{
parent::__construct(
// required
[
'database_type' => 'mysql',
'database_name' => 'phpedu',
'server' => 'localhost',
'username' => 'root',
'password' => 'root'
]
);
}
}
- core/view.php 视图展示数据 platesphp.com/getting-started/simple-example/
// 视图
namespace core;
// Using League\Plates namespace
use League\Plates\Engine;
class View extends Engine
{
public function __construct($path)
{
parent::__construct($path);
}
}
3. 创建应用目录 app,app\models,app\views,app\controllers
- app/models/StaffsModel.php
// 员工模型
namespace models;
// Using core namespace
use core\Model;
// 员工模型
class StaffsModel extends Model
{
public function __construct()
{
parent::__construct();
}
}
- app/views/staffs/list.php
<!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>
- app/views/controllers/StaffsController.php
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]);
}
}
4. 修改 composer.json 将空间映射到目录
- 添加自动加载配置项
{
"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
更新自动加载
5. 入口文件 index.php
// 自动加载
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();
6. 心得体会
core/
核心目录,文件,接管组件功能app/
应用程序目录,分类管理models
管理模型类,和拓展私有功能,扩展组件功能controllers
控制器,集中处理渲染模板的业务逻辑views
视图,模板渲染数据,修改控制器,渲染不同模板