签入版本
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
# 10.2 CLI 命令行使用
|
||||
|
||||
:::tip 概述
|
||||
代码生成器提供 CLI 命令行方式,适合批量生成或 CI/CD 集成场景。
|
||||
代码生成器提供 CLI 命令行方式,适合批量生成或 CI/CD 集成场景。CLI 底层复用 `GeneratorLogic` 与 `GeneratorService`——与 Web 端完全相同的能力,且不受权限与演示模式限制。
|
||||
:::
|
||||
|
||||
## 命令格式
|
||||
@@ -10,87 +10,99 @@
|
||||
# =============================================================================
|
||||
# 代码生成器 CLI 命令
|
||||
# =============================================================================
|
||||
# php think generate:code → ThinkPHP 命令行调用代码生成器
|
||||
# <表名> → 数据库表名(不含表前缀)
|
||||
# |<注释> → 可选,用 | 分隔表注释(用于生成菜单标题)
|
||||
# php artisan generate:code → Artisan 命令行调用代码生成器
|
||||
# <表名> → 数据库表名(含或不含表前缀均可)
|
||||
# |<注释> → 可选,用 | 分隔表注释(用于生成菜单标题)
|
||||
# =============================================================================
|
||||
php think generate:code <表名>[|注释]
|
||||
php artisan generate:code <表名>[|注释]
|
||||
```
|
||||
|
||||
## 参数说明
|
||||
|
||||
| 参数 | 必填 | 说明 | 示例 |
|
||||
|------|------|------|------|
|
||||
| `table`(参数) | 否 | 数据库表名,多表用逗号分隔(需配合 `--batch`) | `think_example` 或 `think_example\|案例演示` |
|
||||
| `table`(参数) | 否 | 数据库表名,多表用逗号分隔(需配合 `--batch`) | `laravel_example` 或 `laravel_example\|案例演示` |
|
||||
| `--config` / `-c` | 否 | 指定已有的 JSON 配置文件路径,跳过表解析 | `--config config.json` |
|
||||
| `--output` / `-o` | 否 | 将配置导出为 JSON 文件 | `--output config.json` |
|
||||
| `--dry-run` / `-d` | 否 | 仅预览,不写入代码文件 | `--dry-run` |
|
||||
| `--type` / `-t` | 否 | 模板类型:`ui`(默认)或 `ui2` | `--type ui2` |
|
||||
| `--overwrite` / `-w` | 否 | 覆盖已存在的文件 | `--overwrite` |
|
||||
| `--type` / `-t` | 否 | 模板类型:`ui`(默认)或 `ui2`(树形) | `--type ui2` |
|
||||
| `--overwrite` / `-w` | 否 | 覆盖已存在的文件(**高危,谨慎使用**) | `--overwrite` |
|
||||
| `--no-menu` | 否 | 不生成菜单 | `--no-menu` |
|
||||
| `--batch` / `-b` | 否 | 批量模式,table 参数支持逗号分隔多表 | `--batch` |
|
||||
|
||||
(完整签名见 `app/Console/Commands/GeneratorCommand.php`。)
|
||||
|
||||
## 使用示例
|
||||
|
||||
```bash
|
||||
# -----------------------------------------------------------------------------
|
||||
# 列出所有可生成的数据库表
|
||||
# -----------------------------------------------------------------------------
|
||||
php think generate:code
|
||||
php artisan generate:code
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 预览生成代码(不写入文件,仅输出到终端)
|
||||
# -----------------------------------------------------------------------------
|
||||
php think generate:code think_example --dry-run
|
||||
php artisan generate:code laravel_example --dry-run
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 生成案例演示模块(带表注释)
|
||||
# 生成案例演示模块(带表注释,注释用于菜单标题)
|
||||
# -----------------------------------------------------------------------------
|
||||
php think generate:code think_example|案例演示
|
||||
php artisan generate:code "laravel_example|案例演示"
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 生成文章管理模块(覆盖已有文件)
|
||||
# -----------------------------------------------------------------------------
|
||||
php think generate:code think_article|文章管理 --overwrite
|
||||
php artisan generate:code "laravel_article|文章管理" --overwrite
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 生成用户管理模块(使用 ui2 树形模板)
|
||||
# 生成部门管理模块(使用 ui2 树形模板)
|
||||
# -----------------------------------------------------------------------------
|
||||
php think generate:code think_user|用户管理 --type ui2
|
||||
php artisan generate:code "laravel_dept|部门管理" --type ui2
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 批量生成多张表
|
||||
# -----------------------------------------------------------------------------
|
||||
php think generate:code think_order,think_product,think_category --batch
|
||||
php artisan generate:code laravel_order,laravel_product,laravel_category --batch
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 导出配置为 JSON 文件
|
||||
# -----------------------------------------------------------------------------
|
||||
php think generate:code think_example --output config.json
|
||||
php artisan generate:code laravel_example --output config.json
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 从配置文件生成(跳过表解析)
|
||||
# -----------------------------------------------------------------------------
|
||||
php think generate:code --config config.json
|
||||
php artisan generate:code --config config.json
|
||||
```
|
||||
|
||||
:::tip Windows 下参数含中文或竖线
|
||||
`表名|注释` 中的 `|` 在部分 shell 中是管道符,请用引号包裹整个参数:`php artisan generate:code "laravel_example|案例演示"`。
|
||||
:::
|
||||
|
||||
## 生成的文件
|
||||
|
||||
执行命令后,自动生成以下文件:
|
||||
|
||||
```text
|
||||
app/controller/ExampleController.php # 控制器(定义 API 接口方法)
|
||||
app/logic/ExampleLogic.php # 业务逻辑(继承 BaseLogic 获得 CRUD)
|
||||
app/model/Example.php # 数据模型(继承 BaseModel 获得软删除/时间戳)
|
||||
app/validate/ExampleValidate.php # 验证器(字段验证规则)
|
||||
ui/src/api/tool/example.ts # 前端 API 接口定义
|
||||
ui/src/views/tool/example/index.vue # 列表页(分页表格 + 搜索)
|
||||
ui/src/views/tool/example/edit.vue # 编辑弹窗(新增/编辑表单)
|
||||
ui/src/views/tool/example/detail.vue # 详情弹窗(只读展示)
|
||||
ui/src/views/tool/example/columns.ts # 表格列配置(仅 ui/ 模板)
|
||||
ui/src/views/tool/example/querySchemas.ts # 查询表单配置(仅 ui/ 模板)
|
||||
app/Http/Controllers/Api/ExampleController.php # 控制器(注解 + 快捷方法)
|
||||
app/Services/ExampleService.php # 业务服务(属性配置 + 钩子骨架)
|
||||
app/Models/Example.php # 数据模型(双 Trait + fillable)
|
||||
app/Http/Requests/ExampleRequest.php # FormRequest(字段验证规则)
|
||||
ui/src/api/tool/example.ts # 前端 API 接口定义
|
||||
ui/src/views/tool/example/index.vue # 列表页(分页表格 + 搜索)
|
||||
ui/src/views/tool/example/edit.vue # 编辑弹窗(新增/编辑表单)
|
||||
ui/src/views/tool/example/detail.vue # 详情弹窗(只读展示)
|
||||
ui/src/views/tool/example/columns.ts # 表格列配置(仅 ui/ 模板)
|
||||
ui/src/views/tool/example/querySchemas.ts # 查询表单配置(仅 ui/ 模板)
|
||||
```
|
||||
|
||||
同时自动完成:
|
||||
|
||||
- `routes/api.php` 插入 CRUD 路由组(9 条标准路由)
|
||||
- `laravel_menu` 创建菜单与权限节点(挂载 `GENERATOR_PARENT_MENU_ID` 指定的父菜单下)
|
||||
- `laravel_dict` / `laravel_dict_item` 创建关联字典(幂等,不重复)
|
||||
|
||||
## 生成后操作
|
||||
|
||||
生成完成后,路由、菜单、字典均已自动创建。通常只需:
|
||||
@@ -99,17 +111,17 @@ ui/src/views/tool/example/querySchemas.ts # 查询表单配置(仅 ui/ 模板
|
||||
# -----------------------------------------------------------------------------
|
||||
# 第 1 步:检查生成的文件是否正确
|
||||
# -----------------------------------------------------------------------------
|
||||
ls app/controller/Example* # 控制器
|
||||
ls app/logic/Example* # 业务逻辑
|
||||
ls app/model/Example* # 数据模型
|
||||
ls app/validate/Example* # 验证器
|
||||
ls ui/src/api/tool/example* # 前端 API
|
||||
ls ui/src/views/tool/example/ # 前端页面
|
||||
ls app/Http/Controllers/Api/Example* # 控制器
|
||||
ls app/Services/Example* # 业务服务
|
||||
ls app/Models/Example* # 数据模型
|
||||
ls app/Http/Requests/Example* # FormRequest
|
||||
ls ui/src/api/tool/example* # 前端 API
|
||||
ls ui/src/views/tool/example/ # 前端页面
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 第 2 步:根据业务需求调整 Logic 属性配置
|
||||
# 第 2 步:根据业务需求调整 Service 属性配置
|
||||
# -----------------------------------------------------------------------------
|
||||
# 可调整:pageLikeFields、pageEqFields、serializeMaps 等
|
||||
# 可调整:pageLikeFields、pageEqFields、serializeMaps、uniqueFields 等
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 第 3 步:在菜单管理中调整菜单排序和图标
|
||||
|
||||
Reference in New Issue
Block a user