hyperf中如何使用command命令行

如果使用过`laravel`和`thinkphp`的话

应该知道可以创建command,也就是可以在cli模式下运行的程序

那么在hyperf中如何实现呢

1、安装`composer require hyperf/command`

2、执行命令生成

`php bin/hyperf.php gen:command FooCommand`

3、打开文件`app/Command/FooCommand.php`

```
container = $container;

parent::__construct('demo:command');
}

public function configure()
{
parent::configure();
$this->setDescription('Hyperf Demo Command');
}

public function handle()
{
echo __method__;
// 从 $input 获取 name 参数
$argument = $this->input->getArgument('name') ?? 'World';
$this->line('Hello ' . $argument, 'info');
$this->line('Hello Hyperf!', 'info');
}

protected function getArguments()
{
return [
['name', InputArgument::OPTIONAL, '这里是对这个参数的解释']
];
}
}
```

4、执行命令`php bin/hyperf.php demo:command`

5、结果如下

```
[root@iZbp13ph356ra22ldly01lZ hyperf-skeleton]# php bin/hyperf.php demo:command hellotheworld
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
App\Command\FooCommand::handle
Hello hellotheworld
Hello Hyperf!
```

    A+
发布日期:2021年12月28日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: