多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 自定义指令 ## 创建自定义指令 第一步,创建一个自定义命令类文件,新建`application/common/command/Hello.php` ``` <?php namespace app\common\command; use think\console\Command; use think\console\Input; use think\console\input\Argument; use think\console\input\Option; use think\console\Output; class Hello extends Command {     protected function configure()     {         $this->setName('hello') ->addArgument('name', Argument::OPTIONAL, "your name") ->addOption('city', null, Option::VALUE_REQUIRED, 'city name') ->setDescription('Say Hello');     }     protected function execute(Input $input, Output $output)     { $name = trim($input->getArgument('name')); $name = $name ?: 'thinkphp'; if ($input->hasOption('city')) { $city = PHP_EOL . 'From ' . $input->getOption('city'); } else { $city = ''; }         $output->writeln("Hello," . $name . '!' . $city);     } } ``` 这个文件定义了一个叫`hello`的命令,并设置了一个`name`参数和一个`city`选项。 第二步,配置`application/command.php`文件 ``` <?php return [     'app\common\command\Hello', ]; ``` 第三步,测试-命令帮助-命令行下运行 ``` php think ``` 输出 ``` Think Console version 0.1 Usage:   command [options] [arguments] Options:   -h, --help            Display this help message   -V, --version         Display this console version   -q, --quiet           Do not output any message       --ansi            Force ANSI output       --no-ansi         Disable ANSI output   -n, --no-interaction  Do not ask any interactive question   -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Available commands:   build              Build Application Dirs   clear              Clear runtime file hello              Say Hello   help               Displays help for a command   list               Lists commands  make   make:controller    Create a new resource controller class   make:model         Create a new model class  optimize   optimize:autoload  Optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production.   optimize:config    Build config and common file cache.   optimize:schema    Build database schema cache. ``` 第四步,运行`hello`命令 ``` php think hello ``` 输出 ``` Hello thinkphp! ``` 添加命令参数 ``` php think hello liuchen ``` 输出 ``` Hello liuchen! ``` 添加`city`选项 ``` php think hello liuchen --city shanghai ``` 输出 ``` Hello thinkphp! From shanghai ``` > 注意看参数和选项的调用区别