Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:
app
应用目录controller
控制器目录models
模型目录views
视图目录core
核心目录composer require catfan/medoo
composer require league/plates
模型类:Model.php
namespace core;
use Medoo\Medoo;
class Model extends Medoo
{
public function __construct()
{
parent::__construct([
'database_type' => 'mysql',
'database_name' => 'phpedu',
'server' => 'localhost',
'username' => 'root',
'password' => '123456'
]);
}
public function first() {
// 自定义方法
}
}
视图类:View.php
namespace core;
use League\Plates\Engine;
class View extends Engine
{
public $templates;
public function __construct($path)
{
$templates = parent::__construct($path);
}
}
controllers
models
views
目录UserController.php
文件,获取用户数据赋值给模板
namespace controllers;
class UserController
{
public $model;
public $view;
public function __construct($model,$view)
{
$this->model = $model;
$this->view = $view;
}
public function index()
{
return __METHOD__;
}
public function select()
{
// 获取数据
$users = $this->model->select('users',['sid','name','gender','salary','email'],['salary[>=]'=>5000,'LIMIT'=> 8]);
// 模板赋值
return $this->view->render('users/list',['users'=> $users]);
}
}
UserModel.php
获取用户表数据
namespace models;
use core\Model;
class UserModel extends Model
{
public function __construct()
{
parent::__construct();
}
}
users
在新建目录下创建list.php
文件显示用户数据视图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<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: lightcoral;
}
</style>
</head>
<body>
<h3>用户管理系统</h3>
<table>
<tr>
<th>UID</th>
<th>用户名</th>
<th>性别</th>
<th>工资</th>
<th>邮箱</th>
<th>操作</th>
</tr>
<?php foreach ($users as $user): ?>
<tr>
<td><?=$this->e($user['sid'])?></td>
<td><?=$this->e($user['name'])?></td>
<td><?=$this->e($user['gender']) == 'male' ? '男' : '女'?></td>
<td><?=$this->e($user['salary'])?></td>
<td><?=$this->e($user['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>
</p>
</body>
</html>
{
"require": {
"catfan/medoo": "^1.7",
"league/plates": "^3.4"
},
"autoload": {
"psr-4": {
"models\\": "app/models",
"views\\": "app/views",
"controllers\\": "app/controllers",
"core\\": "core"
}
}
}
composer dump
更新自动加载
// 入口文件
use controllers\UserController;
use models\UserModel;
use core\View;
require __DIR__ . '/vendor/autoload.php';
// 模型
$model = new UserModel();
// 视图
$view = new View('app/views');
// 控制器
$controller = new UserController($model,$view);
print_r($controller->select());
总结:
1.使用composer第三方包依赖快速创建Model与View
2.创建core核心文件目录
3.创建App目录,严格按照MVC架构创建三个文件夹Models、Views、Controllers
4.composer.js映射autoload目录时需注意在其他文件use导入时的名称