###怎么搞
* 編輯`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. 執行服務器后臺任務