
批改狀態(tài):合格
老師批語:
依照課堂案例,試著自己寫一個迷你的MVC小框架,流程走通即可,并寫出詳細步驟和心得體會
修改數(shù)據(jù)庫 id 字段為 sid alter table staffs change id sid int unsigned auto_increment
vscode 打開終端進入創(chuàng)建的 0304-frame 目錄
終端 composer search medoo
搜索結(jié)果找到
catfan/medoo The lightweight PHP database framework to accelerate development
composer require catfan/medoo
composer search plates
搜索結(jié)果找到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 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'
]
);
}
}
// 視圖
namespace core;
// Using League\Plates namespace
use League\Plates\Engine;
class View extends Engine
{
public function __construct($path)
{
parent::__construct($path);
}
}
// 員工模型
namespace models;
// Using core namespace
use 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>用戶管理系統(tǒng)</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() {
// 獲取數(shù)據(jù)
// 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/
應(yīng)用程序目錄,分類管理models
管理模型類,和拓展私有功能,擴展組件功能controllers
控制器,集中處理渲染模板的業(yè)務(wù)邏輯views
視圖,模板渲染數(shù)據(jù),修改控制器,渲染不同模板微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號