框架目录结构:
- app: 应用目录
- controllers: 控制器目录
- models: 模型目录
- views: 视图目录
- core: 核心目录
- vendor: Composer类库目录
- composer.json: Composer 定义文件
具体步骤如下:
1. 使用第三方包下载
Model: composer require catfan/medoo 得到一个 vendor/catfan/medoo文件
View: composer require league/plates 得到一个 vendor/league/plates文件
2. 打开配置文件composer.json
样式代码:
{
"name": "zhu/php.cn",
"description": "this is a test",
"require": {
"catfan/medoo": "^1.7",
"league/plates": "^3.4"
},
"autoload": {
"psr-4": {
"models\\": "app/models",
"views\\": "app/views",
"controllers\\": "app/controllers",
"core\\": "core"
}
}
}
3.新建core核心目录并在其下新建模型: Model 和视图: View 两个文件
Model.php样式代码:
<?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' => 'root',
]);
}
public function first()
{
// 自定义方法
}
}
View.php样式代码:
<?php
namespace core;
use League\Plates\Engine;
class View extends Engine
{
public $templates;
public function __construct($path)
{
$templates = parent::__construct($path);
}
}
4. 在app目录下新建controllers models views 三个目录
4.1 在controllers目录下新建StaffsController.php文件,用来获取用户数据赋值给模板
StaffsController.php样式代码:
<?php
namespace controllers;
class StaffsController
{
public $model;
public $view;
public function __construct($model, $view)
{
$this->model = $model;
$this->view = $view;
}
public function index()
{
return __METHOD__;
}
public function select()
{
// 获取数据
$staffs = $this->model->select('staffs', ['id', 'name', 'gender', 'salary', 'email'], ['salary[>=]' => 6000, 'LIMIT' => 6]);
// 模板赋值
return $this->view->render('staffs/list', ['staffs' => $staffs]);
}
}
4.2 在Models目录下新建StaffsModel.php文件,用来获取表数据
StaffsModel.php样式代码:
<?php
namespace models;
use core\Model;
// 自定义模型类通常与一张数据表绑定,继承自框架核心模型类
class StaffsModel extends Model
{
public function __construct()
{
parent::__construct();
}
}
4.3 在Views目录下新建一个staffs文件夹,在该文件夹下面新建list.php文件
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: #0ff;
}
</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 $uname): ?>
<tr>
<td><?=$this->e($uname['id'])?></td>
<td><?=$this->e($uname['name'])?></td>
<td><?=$this->e($uname['gender']) == 'male' ? '男' : '女'?></td>
<td><?=$this->e($uname['salary'])?></td>
<td><?=$this->e($uname['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>
5. composer.json 将控件映射到目录
composer.json样式代码: 完成后开启终端输入 composer dump 更新自动加载
{
"name": "zhu/php.cn",
"description": "this is a test",
"require": {
"catfan/medoo": "^1.7",
"league/plates": "^3.4"
},
"autoload": {
"psr-4": {
"models\\": "app/models",
"views\\": "app/views",
"controllers\\": "app/controllers",
"core\\": "core"
}
}
}
6. index.php 入口文件
<?php
// 入口文件
use models\StaffsModel;
use controllers\StaffsController;
use core\View;
require __DIR__ . '/vendor/autoload.php';
// 测试模型
$model = new StaffsModel();
// 测试视图
$view = new View('app/views');
// 测试控制器
$controller = new StaffsController($model,$view);
print_r($controller->select());
运行效果预览:
总结:
第一步先安装配置好 composer 下载地址: https://pkg.phpcomposer.com/
第二步找到composer所需的第三方包依赖进行快速搭建模型Model与视图View
第三步 创建一个核心目录文件夹
第四步 创建app目录,搭建起MVC架构
第五步 配置好composer.js文件,用来映射目录文件
index.php 是入口文件得注意use导入名称信息