# 10.1 概述与架构 :::tip 概述 代码生成器可根据数据库表结构一键生成 CRUD 模块的全部代码,包括后端 Controller/Logic/Model/Validate 和前端 API/View。支持 MySQL、PostgreSQL、SQL Server、Oracle、SQLite 五种数据库。 ::: ## 使用方式 ### 方式一:CLI 命令行 ```bash # ----------------------------------------------------------------------------- # 代码生成器 CLI 命令 # ----------------------------------------------------------------------------- # php think generate:code → 调用 ThinkPHP 命令行的代码生成器 # Example "案例演示" → 表名与注释(用 | 分隔,如 think_example|案例演示) # --dry-run → 仅预览,不写入文件 php think generate:code think_example|案例演示 --dry-run ``` 详见 [CLI 命令行使用](/zh/generator/cli.html)。 ### 方式二:Web 管理界面 进入「工具管理 → 代码生成」菜单,可视化操作。 详见 [Web 管理界面](/zh/generator/web-ui.html)。 ## 生成流程 ```text ┌──────────────┐ │ 数据库表 │ 如 think_example └──────┬───────┘ │ ▼ ┌──────────────┐ │ GeneratorService│ 读取表字段信息(多数据库适配) │ ::getTableList()│ └──────┬───────┘ │ ▼ ┌──────────────┐ │ GeneratorService│ 字段类型归一化 + 属性分析 │ ::getTableColumns│ (图片、富文本、筛选项、唯一等) └──────┬───────┘ │ ▼ ┌──────────────┐ │ GeneratorService│ 模板变量构建 + 模板渲染 │ ::preview() │ └──────┬───────┘ │ ▼ ┌──────────────┐ │ GeneratorService│ 文件生成 + 菜单创建 │ ::generate() │ └──────┬───────┘ │ ┌────┼────┬────┬────┐ ▼ ▼ ▼ ▼ ▼ ┌────┐┌────┐┌────┐┌────┐┌────┐ │Ctrl││Logi││Mode││Vali││API │ │ . ││ c ││ l ││date││+Vue│ │php ││php ││php ││php ││ │ └────┘└────┘└────┘└────┘└────┘ ``` ## 核心能力 | 能力 | 说明 | |------|------| | 多数据库适配 | MySQL / PostgreSQL / SQL Server / Oracle / SQLite | | 字段分析 | 自动识别图片字段、富文本字段、筛选字段、唯一字段 | | 模板渲染 | 基于 PHP 模板引擎,支持自定义模板 | | 菜单创建 | 自动生成菜单和权限节点 | | 字典创建 | 自动创建关联字典(含字典项),幂等不重复 | | 路由注册 | 自动在 route/app.php 中插入 CRUD 路由组 | | 安全校验 | 表名合法性校验,防止 SQL 注入 | | CLI 命令行 | `php think generate:code` 命令行操作,支持预览、导出配置、批量生成 | ## API 接口 | 方法 | 路径 | 权限码 | 说明 | |------|------|--------|------| | GET | `/api/generator/page` | sys:generator:page | 获取可生成的表列表 | | POST | `/api/generator/preview` | sys:generator:generate | 预览生成代码(不写入文件) | | POST | `/api/generator/generate` | sys:generator:generate | 一键生成 | | POST | `/api/generator/batchGenerate` | sys:generator:generate | 批量生成 | ## CLI 命令行 除 Web 接口外,还提供 CLI 命令行工具,适合开发环境和批量操作: ```bash # ----------------------------------------------------------------------------- # 列出所有可生成的数据库表 # ----------------------------------------------------------------------------- php think generate:code # ----------------------------------------------------------------------------- # 预览生成代码(不写入文件,仅输出到终端) # ----------------------------------------------------------------------------- # --dry-run → 干运行模式(只预览不执行) php think generate:code think_example --dry-run # ----------------------------------------------------------------------------- # 一键生成(实际写入文件到 app/ 和 ui/src/) # ----------------------------------------------------------------------------- php think generate:code think_example # ----------------------------------------------------------------------------- # 导出配置(将生成参数保存为 JSON,便于重复使用) # ----------------------------------------------------------------------------- # --output config.json → 输出配置文件路径 php think generate:code think_example --output config.json # ----------------------------------------------------------------------------- # 从配置生成(读取之前导出的配置文件) # ----------------------------------------------------------------------------- # --config config.json → 读取配置文件 php think generate:code --config config.json ``` 详见 [10.2 CLI 命令行使用](/zh/generator/cli.html) :::tip 演示模式 preview 接口标注了 `#[DemoAllow]`,演示环境下也可使用。generate 和 batchGenerate 在演示环境下会被拦截。 ::: ## 核心代码 ```php // app/controller/GeneratorController.php — 代码生成器控制器 class GeneratorController extends BaseController { protected GeneratorLogic $logic; protected function initialize(): void { $this->logic = new GeneratorLogic(); // 初始化代码生成器业务逻辑 } // 预览生成代码(不写入文件,仅返回渲染结果) #[Log('代码生成-预览代码', Log::TYPE_QUERY)] #[Permission('sys:generator:generate', '代码生成')] #[DemoAllow] // 演示模式允许(只读操作) public function preview(): Json { try { $params = $this->getJsonBody(); // 获取前端传入的 JSON 参数 $result = $this->logic->preview($params); // 调用逻辑层预览方法 return $this->success($result); } catch (\Throwable $e) { // 返回详细错误信息(包含文件名和行号,便于调试) return $this->fail($e->getMessage() . ' [File: ' . $e->getFile() . ', Line: ' . $e->getLine() . ']'); } } // 一键生成(实际写入文件到 app/ 和 ui/src/ 目录) #[Log('代码生成-一键生成', Log::TYPE_GENERATE)] #[Permission('sys:generator:generate', '代码生成')] public function generate(): Json { try { $params = $this->getJsonBody(); $result = $this->logic->generate($params); // 调用逻辑层生成方法 $generated = count($result['generated']); // 成功生成的文件数 $skipped = count($result['skipped']); // 跳过的文件数(已存在) $msg = "生成完成:成功 {$generated} 个文件"; if ($skipped > 0) { $msg .= ",跳过 {$skipped} 个文件"; } return $this->success($result, $msg); } catch (\Throwable $e) { return $this->fail($e->getMessage()); } } } ``` ## 模板文件 ```text templates/ # 代码生成器模板目录 ├── controller.php.tpl # 控制器模板 ├── logic.php.tpl # 业务逻辑模板(含 serializeMaps、导入导出) ├── model.php.tpl # 数据模型模板 ├── validate.php.tpl # 验证器模板 ├── ui/ # 普通列表模板(6个前端文件) │ ├── api.ts.tpl # API 接口模块 │ ├── index.vue.tpl # 列表页 │ ├── edit.vue.tpl # 编辑弹窗 │ ├── detail.vue.tpl # 详情弹窗 │ ├── columns.ts.tpl # 表格列配置 │ └── querySchemas.ts.tpl # 查询表单配置 └── ui2/ # 树形结构模板(4个前端文件) ├── api.ts.tpl ├── index.vue.tpl ├── edit.vue.tpl └── detail.vue.tpl ```