编辑command.php
return [ 'app\[模块]\console\command\[命令名]', ];
2.建立[模块]/console/command/[命令名].php
namespace app\[模块]\console\command; use think\console\command; use think\console\Input; use think\console\Output; class [命令名] extends command\Command { // 定义命令提示和参数 protected function configure(){ parent::configure(); //具体用法请自行查看thinkphp/library/think/console/command/Command.php $this->setName([命令名])->setDescription([命令描述]); } // 执行命令 protected function execute(Input $input, Output $output){ //$input用户获取命令行环境里的输入 //$output->write($messages, $newline = false, $type = self::OUTPUT_NORMAL),向命令行输出信息 //$output->writeln($messages, $type = self::OUTPUT_NORMAL),向命令行输出单行信息 /** * 命令行交互获取用户输入 **/ $output->writeln('Input something:'); $handle = fopen("php://stdin","r"); $input = trim(fgets($handle)); $output->writeln('Output: '.$input); } }
3.运行命令行
在打开操作系统命令行,在项目根目录运行php think
可以看到刚才定义的命令