ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
###怎么搞 * 编辑`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(); $this->setName([命令名])->setDescription([命令描述]); //具体用法请自行查看thinkphp/library/think/console/command/Command.php } // 执行命令 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`可以看到刚才定义的命令 * * * * * ###应用 1. 需要编写安装程序,但是嫌编写GUI安装程序麻烦 2. 通过命令行一键清除缓存、session等临时数据 3. 执行服务器后台任务