DIY 实现 ThinkPHP 核心框架 (一)MVC
ThinkPHP
框架目前已经到了 6
,与 5
相比,目录结构有所改变, 5
的核心框架在根目录 thinkphp
下,而 6
放在了 vendor
的 topthink
。6
改进了中间件, ORM
组件独立等等。但是他们的基本框架原理是一致的,比如支持 MVC
开发模式,都采用了路由、容器、控制反转、依赖注入功能,使用 composer
实现类的自动加载等等。熟悉 ThinkPHP
框架原理对使用 5
或者 6
乃至以后的 7
都会更得心应手。
什么是 MVC
?
MVC
是一种使用MVC
(Model View Controller
模型-视图-控制器)设计创建Web
应用程序的模式。Model
(模型)是应用程序中用于处理应用程序数据逻辑的部分。通常模型对象负责在数据库中存取数据。View
(视图)是应用程序中处理数据显示的部分。通常视图是依据模型数据创建的。Controller
(控制器)是应用程序中处理用户交互的部分。通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据。
搭建 LNMP
具体如何搭建 LNMP
平台可以参考 Linux 开发环境 。本实验环境参数为
CentOS Linux release 7.8
PHP 7.4.0
Nginx/1.18.0
Mysql Ver 14.14 Distrib 5.7.31
Xhell 5
Navicat premium 15
VS code 1.48.1
Phpstorm 2020.1
创建虚拟主机
server {
listen 80;
server_name diy.tp;
access_log logs/diy.tp..access.log main;
error_log logs/diy.tp..error.log;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location \ {
try_files $uri $uri/ /index.html index.php;
}
location ~ ^(.+\.php)(.*)$ {
# 实验根目录所在路径,起始测试文件放于此
root /home/web/mytp/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# 设置 PATH_INFO 获取 url 中的模块、控制器和方法
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
创建测试数据
// 创建数据库
CREATE DATABASE tp;
// 创建数据表
CREATE TABLE `employees` (
`empNum` int(10) unsigned PRIMARY KEY NOT NULL AUTO_INCREMENT COMMENT '员工 id',
`name` varchar(10) NOT NULL DEFAULT ' ' COMMENT '姓名',
`gender` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '性别',
`mobile` varchar(20) NOT NULL DEFAULT ' ' COMMENT '手机号',
`email` varchar(40) NOT NULL DEFAULT ' ' COMMENT '邮箱',
) ENGINE=InnoDB DEFAULT CHARSET=utf8
// 插入测试数据
INSERT INTO `employees` VALUES (5000, 'Bob', 1, '12345678900', 'bob@thinkphp.demo');
INSERT INTO `employees` VALUES (5001, 'David', 1, '12345678901', 'david@thinkphp.demo');
INSERT INTO `employees` VALUES (5002, 'Michael', 1, '12345678902', 'michael@thinkphp.demo');
INSERT INTO `employees` VALUES (5003, 'Lily', 0, '12345678903', 'lily@thinkphp.demo');
INSERT INTO `employees` VALUES (5004, 'Lucy', 0, '12345678904', 'lucy@thinkphp.demo');
INSERT INTO `employees` VALUES (5005, 'Rose', 0, '12345678905', 'rose@thinkphp.demo');
INSERT INTO `employees` VALUES (5006, 'Merry', 0, '12345678906', 'merry@thinkphp.demo');
创建视图 employee.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<table border="2" cellpadding="1" cellspacing="9">
<tr><th>员工编号</th><th>姓名</th><th>性别</th><th>手机号</th><th>邮箱</th></tr>
// 可以直接使用控制器中的变量 $data
<?php foreach ($data as $row): ?>
<tr>
<td><?=$row['empNum'] ?></td>
<td><?=$row['name'] ?></td>
<td><?=['女', '男'][$row['gender']] ?></td>
<td><?=$row['mobile'] ?></td>
<td><?=$row['email'] ?></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
创建模型 EmployeeModel.php
class EmployeeModel
{
protected $link;
// 连接数据库
public function __construct()
{
$this->link = new mysqli('127.0.0.1', 'root', '123456', 'tp');
$this->link->set_charset('utf8');
}
// 获取数据
public function getAll()
{
$sql = "SELECT * FROM employees";
$res = $this->link->query($sql);
return $res->fetch_all(MYSQLI_ASSOC);
}
}
创建控制器 EmployeeController.php
class EmployeeController
{
public function index()
{
require 'EmployeeModel.php';
$model = new EmployeeModel();
// 此数据将被用于视图 <table> 表格中
</table>
$data = $model->getAll();
require 'employee.html';
}
}
创建 index.php
require 'EmployeeController.php';
$employee = new EmployeeController();
$employee->index();
此时目录结构
/home/web/mytp/public/
EmployeeController.php
employee.html
EmployeeModel.php
index.php
访问 http://diy.tp/index.php
,运行结果为
本作品采用《CC 协议》,转载必须注明作者和本文链接