[TOC] #### 1. 命令行介绍 --- 在 ThinkPHP 中,自定义指令(Command)是处理后台任务、定时统计、数据同步等耗时操作的核心工具 + 它将业务逻辑从 Web 请求中解耦,通过命令行直接调用,避免了浏览器端的超时问题 常用的 ThinkPHP 内置命令: ```bash php think run php think version php think make:controller Blog ``` ThinkPHP 提供了强大的命令行工具 `think`,除了框架自带的命令外,还可以编写自己的命令,用于: + 定时任务 + 数据同步 + 数据统计 + 队列消费 + 日志清理 其作用类似于 Laravel 的 Artisan 命令 #### 2. 创建指令类文件 --- 推荐使用内置命令快速生成指令骨架,语法格式: ```plaintext php think make:command 自定义命令类 命令名 ``` 使用示例 ```bash # 单应用模式 php think make:command Hello hello php think make:command RabbitmqConsumer rabbitmq:consumer # 多应用模式 php think make:command index@Hello hello php think make:command index@RabbitmqConsumer rabbitmq:consumer ``` 一个标准的自定义指令类需要继承 `think\console\Command`,并实现以下两个核心方法: + `configure()`:用于定义指令的名称、描述以及接收的参数和选项 + `execute(Input $input, Output $output)`:指令的执行入口,包含具体的业务逻辑 ```php protected function configure() { // 指令配置 $this->setName('hello') ->setDescription('the hello command'); } protected function execute(Input $input, Output $output) { // 指令输出 $output->writeln('hello'); } ``` #### 3. 注册自定义命令 --- 必须在配置文件中注册指令才能生效,打开 `config/console.php`,在 `commands` 数组中添加: + 有键名:键名就是命令名 + 没有键名,命令名自动从对应的自定义指令类名中获取,也就是传入 `setName()` 方法的参数 ```php // 指令定义 'commands' => [ 'hello' => app\command\Hello::class, // 'hello' => app\index\command\Hello::class, // 可以这样定义,命令名称会自动从类中获取,也就是传入 setName 方法的参数 rabbitmq:consumer \app\command\RabbitmqConsumer::class, // 也可以自定义命令名称 // 'consumer' => \app\command\RabbitmqConsumer::class, ], ``` 查看所有可用的命令(验证自定义命令是否成功注册) ```bash php think list ``` 测试运行: ```bash php think hello ``` #### 4. 指令的参数、选项 --- 指令参数 ```php protected function configure() { // 指令配置 $this->setName('hello') // 声明参数:名称、模式(可选/必写)、描述、默认值 // 模式取值:Argument::OPTIONAL 可选参数 Argument::REQUIRED 必写参数 ->addArgument('name', Argument::OPTIONAL, "你的名字", 'liang') ->addArgument('age', Argument::OPTIONAL, "你的年龄", '20') // 指令描述 ->setDescription('你好'); } protected function execute(Input $input, Output $output) { $name = $input->getArgument('name'); $age = $input->getArgument('age'); $output->writeln("你好,您的名称:{$name},年龄:{$age}"); } ``` 使用示例: ```bash # 不带参数,使用参数的默认值 php think hello # 带参数(name: wang) php think hello wang # 带参数(name: wang age: 25) php think hello wang 25 ``` 指令选项 ```php protected function configure() { // 指令配置 $this->setName('hello') // 声明选项:名称、简写(短名称)、模式(无值/必传值/可选值)、描述、默认值 ->addOption('force', 'f', Option::VALUE_NONE, "force reimport") ->addOption('limit', 'l', Option::VALUE_OPTIONAL, "limit the number of imported items", 100) // 指令描述 ->setDescription('你好'); } protected function execute(Input $input, Output $output) { if ($input->hasOption('force')) { $output->writeln("强制重新导入"); } $limit = $input->getOption('limit'); $output->writeln("限制导入条数:{$limit}"); } ``` 使用示例: ```bash php think hello -f php think hello -l 10 php think hello -f -l 10 ``` 查看命令帮助可以看到参数和选项说明(描述最好用英文,不然在终端显示可能会乱码) ```bash # 以下两个命令等价 php think hello -h php think help hello ``` #### 5. 彩色输出 --- ```php // 输出信息 $output->write("Hello, ThinkPHP6!"); // 输出信息并换行 $output->writeln("Hello, ThinkPHP6!"); // 五种彩色输出 $output->writeln('<info>执行成功</info>:日志清理完成'); $output->writeln('<error>执行失败</error>'); $output->writeln('<question>question</question>'); $output->writeln('<highlight>highlight</highlight>'); $output->writeln('<warning>warning</warning>'); ``` #### 6. 开发中的最佳实践 --- 对于 ThinkPHP8 项目,建议遵循以下规范(单应用模式): ```plaintext app/command ├── UserSync.php ├── OrderClear.php ├── DataBackup.php ├── QueueConsume.php └── SendEmail.php ``` 命令名称统一使用: ```plaintext 模块:动作 ``` 例如: ```plaintext user:sync order:clear order:refund queue:work email:send ``` 这样在项目规模增大后,命令列表依然清晰易维护