ThinkPHP各版本均支持命令行快速生成代码:TP5.1用php think make:controller Index等生成基础骨架;TP6.x需用api/v1.User语法指定命名空间;TP8.x新增--rest参数一键生成7方法资源控制器,并支持service、migration等新类型。

用命令行快速生成控制器、模型、验证器等骨架代码,避免手动创建文件和写重复结构,尤其适合ThinkPHP 5.1、6.x、8.x多个版本的日常开发节奏。
确认当前项目使用的ThinkPHP版本
打开终端,进入项目根目录,执行:php think version。这一步必须做,因为不同版本的命令行参数、生成路径、命名规则完全不同,【TP6.x默认不识别tp5的think命令写法】。
若提示“Command not found”,说明未正确安装或入口文件缺失——检查think文件是否存在且具备可执行权限(Linux/macOS需chmod +x think)。
ThinkPHP 5.1 命令行核心用法
方法一:生成控制器
执行php think make:controller Index,会在application/index/controller/下生成Index.php,类名自动为Index,继承Controller基类。
立即学习“PHP免费学习笔记(深入)”;
方法二:生成模型php think make:model User → 生成application/common/model/User.php(注意:5.1默认走common模块,不是app),类名User,继承Model。
方法三:生成验证器php think make:validate UserValidate → 生成application/common/validate/UserValidate.php,含rule和message属性模板。
ThinkPHP 6.x 命令行升级要点
第一步:确认命令入口已更新
TP6使用think脚本而非index.php?s=方式,且必须位于项目根目录;若从TP5升级,旧think文件不可直接复用,需重新下载或执行composer update topthink/framework拉取新版入口。
第二步:生成控制器(带命名空间)php think make:controller api/v1.User → 自动生成app/api/controller/v1/User.php,命名空间为app\api\controller\v1,类名User。斜杠/会被转为子目录,点.决定类名,这是TP6特有语法。
第三步:生成模型并指定连接php think make:model app\common\model\User --connection=mysql → 强制指定数据库连接配置项名,否则默认读database.default,【若配置中无default键,会静默失败且不报错】。
ThinkPHP 8.x 新增能力速查
支持一键生成RESTful资源控制器:php think make:controller --rest User,直接产出包含index、create、save、read、edit、update、delete七种方法的标准资源控制器。
支持生成服务类:php think make:service User → 在app/service/下创建UserService.php,自动注入Container实例,并预留__construct方法签名。
支持批量生成迁移文件:php think make:migration create_users_table --table=users → 生成带up和down方法的迁移类,字段定义留空待手动填充,避免手敲基础框架。



















