文章目录
- 前言
-
- [📦 ThinkPHP 模块结构](#📦 ThinkPHP 模块结构)
- [🎯 创建新模块的步骤](#🎯 创建新模块的步骤)
-
- [步骤1:在 application 目录下创建模块文件夹](#步骤1:在 application 目录下创建模块文件夹)
- 步骤2:创建控制器文件
- 步骤3:创建视图文件
- 步骤4:访问新模块
- [📝 更完整的模块示例](#📝 更完整的模块示例)
-
- [1. 创建模型 `d:\php\ht\application\test\model\User.php`](#1. 创建模型
d:\php\ht\application\test\model\User.php) - [2. 创建控制器 `d:\php\ht\application\test\controller\User.php`](#2. 创建控制器
d:\php\ht\application\test\controller\User.php) - [3. 创建视图文件](#3. 创建视图文件)
- [1. 创建模型 `d:\php\ht\application\test\model\User.php`](#1. 创建模型
- [🔑 访问路径总结](#🔑 访问路径总结)
- [⚠️ 注意事项](#⚠️ 注意事项)
前言
📦 ThinkPHP 模块结构
首先看看现有项目的结构:
d:\php\ht\application\
├── admin/ ← 后台管理模块
│ ├── controller/ ← 控制器(处理请求)
│ ├── model/ ← 模型(操作数据库)
│ └── view/ ← 视图(显示页面)
│ └── config/ ← 模块配置
│
├── api/ ← API接口模块
│ ├── controller/
│ ├── model/
│ └── view/
│
└── chat/ ← 聊天模块
🎯 创建新模块的步骤
假设我们要创建一个 "Test" 测试模块
步骤1:在 application 目录下创建模块文件夹
在 d:\php\ht\application\ 下创建:
test/ ← 新模块目录
├── controller/ ← 控制器目录
├── model/ ← 模型目录
├── view/ ← 视图目录
│ └── test/ ← 视图子目录(与控制器对应)
│ └── index.html ← 视图文件
└── config/ ← 模块配置目录
步骤2:创建控制器文件
在 d:\php\ht\application\test\controller\ 下创建 Index.php
php
<?php
namespace app\test\controller;
use think\Controller;
class Index extends Controller
{
// 首页方法
public function index()
{
return $this->fetch();
}
// 欢迎方法
public function hello($name = 'World')
{
return 'Hello, ' . $name . '!';
}
}
步骤3:创建视图文件
在 d:\php\ht\application\test\view\test\ 下创建 index.html
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test 模块</title>
</head>
<body>
<h1>欢迎来到 Test 模块!</h1>
<p>这是我的第一个 ThinkPHP 模块</p>
</body>
</html>
步骤4:访问新模块
创建完成后,访问地址:
http://flyenv-test-oqbbixhm.test/test ← 访问 index 方法
http://flyenv-test-oqbbixhm.test/test/index ← 同上
http://flyenv-test-oqbbixhm.test/test/hello ← 访问 hello 方法
📝 更完整的模块示例
让我给你演示一个带完整功能的模块(增删改查):
1. 创建模型 d:\php\ht\application\test\model\User.php
php
<?php
namespace app\test\model;
use think\Model;
class User extends Model
{
// 设置表名(可选,默认会解析)
protected $name = 'user';
// 自动写入时间戳(可选)
protected $autoWriteTimestamp = true;
}
2. 创建控制器 d:\php\ht\application\test\controller\User.php
php
<?php
namespace app\test\controller;
use think\Controller;
use app\test\model\User as UserModel;
class User extends Controller
{
// 用户列表页面
public function lists()
{
// 查询所有用户
$users = UserModel::select();
// 分配给视图
$this->assign('users', $users);
// 渲染视图
return $this->fetch();
}
// 添加用户表单页面
public function add()
{
return $this->fetch();
}
// 处理添加用户
public function doAdd()
{
// 获取POST数据
$data = input('post.');
// 创建用户
$user = new UserModel();
$user->save($data);
// 返回成功
return '用户添加成功!';
}
// 编辑用户
public function edit($id)
{
// 查询用户
$user = UserModel::find($id);
// 分配给视图
$this->assign('user', $user);
return $this->fetch();
}
// 删除用户
public function doDel($id)
{
UserModel::destroy($id);
return '删除成功!';
}
}
3. 创建视图文件
列表页 d:\php\ht\application\test\view\user\lists.html
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户列表</title>
</head>
<body>
<h1>用户列表</h1>
<a href="/test/user/add">添加用户</a>
<table border="1">
<tr>
<th>ID</th>
<th>用户名</th>
<th>操作</th>
</tr>
<?php foreach($users as $user): ?>
<tr>
<td><?php echo $user['id']; ?></td>
<td><?php echo $user['username']; ?></td>
<td>
<a href="/test/user/edit/id/<?php echo $user['id']; ?>">编辑</a>
<a href="/test/user/doDel/id/<?php echo $user['id']; ?>">删除</a>
</td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
添加页 d:\php\ht\application\test\view\user\add.html
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<h1>添加用户</h1>
<form method="post" action="/test/user/doAdd">
用户名:<input type="text" name="username"><br><br>
密码:<input type="password" name="password"><br><br>
<button type="submit">提交</button>
</form>
</body>
</html>
🔑 访问路径总结
php
┌────────────────────────────────────────────────────────┐
│ URL 路径 → 控制器 → 方法 │
├────────────────────────────────────────────────────────┤
│ /test → Index → index() │
│ /test/index → Index → index() │
│ /test/user/lists → User → lists() │
│ /test/user/add → User → add() │
│ /test/user/doAdd → User → doAdd() │
│ /test/user/edit/id/5 → User → edit(id=5) │
└────────────────────────────────────────────────────────┘
⚠️ 注意事项
- 控制器文件名必须和类名一致,且首字母大写
- 视图目录要和控制器名对应(User控制器 → view/user/)
- 视图文件要和控制器方法名对应(edit方法 → edit.html)
- 如果出现404,检查URL重写是否配置正确