ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 控制台命令 * [控制台命令](https://www.kancloud.cn/uuling/tp5-console/366131#_0) * [创建一个命令](https://www.kancloud.cn/uuling/tp5-console/366131#_4) * [配置命令](https://www.kancloud.cn/uuling/tp5-console/366131#_34) * [执行命令](https://www.kancloud.cn/uuling/tp5-console/366131#_74) * [控制台输入](https://www.kancloud.cn/uuling/tp5-console/366131#_156) * [命令的生命周期](https://www.kancloud.cn/uuling/tp5-console/366131#_213) ## 创建一个命令 > 命令通过类来定义,一个合法的命令类,没有固定的目录和命名空间要求,但必须继承`think\console\command\Command`或者其子类,并且定义`configure`和`execute`两个方法。 例如,一个名为 CreateUser 的命令必须遵循此结构: ~~~ <?php // src/application/console/CreateUser.php namespace app\console; use think\console\Command; use think\console\Input; use think\console\Output; class CreateUser extends Command { protected function configure() { // ... } protected function execute(Input $input, Output $output) { // ... } } ~~~ ## 配置命令 > 首先,你必须在`configure()`方法中配置命令的名称,然后可选地定义一个帮助信息 和 输入选项及输入参数: ~~~ <?php // src/application/console/CreateUser.php namespace app\console; use think\console\Command; use think\console\Input; use think\console\Output; class CreateUser extends Command { // ... protected function configure() { $this // 命令的名字("think" 后面的部分) ->setName('app:create-user') // 运行 "php think list" 时的简短描述 ->setDescription('Create new users.') // 运行命令时使用 "--help" 选项时的完整命令描述 ->setHelp("This command allows you to create users...") ; } protected function execute(Input $input, Output $output) { } } ~~~ ## 执行命令 配置命令之后,然后在`application`目录下面的`command.php`(如果不存在则创建)文件中添加如下内容: ~~~ <?php return [ \app\console\CreateUser::class, ]; ~~~ 你就能在终端(terminal)中执行它: ~~~ $ php think app:create-user ~~~ 你可能已经知道,这个命令将什么也不做,因为你还没有写入任何逻辑。在`execute()`方法里添加你自己的逻辑,这个方法可以访问到`input stream`(如,选项和参数)和`output stream`(以写入信息到命令行): ~~~ <?php // src/application/console/CreateUser.php namespace app\console; use think\console\Command; use think\console\Input; use think\console\Output; class CreateUser extends Command { // ... protected function configure() { $this // 命令的名字("think" 后面的部分) ->setName('app:create-user') // 运行 "php think list" 时的简短描述 ->setDescription('Creates new users.') // 运行命令时使用 "--help" 选项时的完整命令描述 ->setHelp("This command allows you to create users...") ; } protected function execute(Input $input, Output $output) { // 输出多行到控制台(每一行的末尾添加 "\n") $output->writeln([ 'Create User', '============', '', ]); // 输出消息后面跟着一个 "\n" $output->writeln('Whose!'); // 输出消息但并不在行的结尾加上 "\n"。 $output->write('You are about to '); $output->write('create a user.'); } } ~~~ 现在,尝试执行此命令: ~~~ $ php think app:create-user User Creator ============ Whoa! You are about to create a user. ~~~ ## 控制台输入 使用`input`选项或参数来传入信息给命令: ~~~ <?php namespace app\console; use think\console\Command; use think\console\Input; use think\console\input\Argument; use think\console\Output; class CreateUser extends Command { // ... protected function configure() { $this // 命令的名字("think" 后面的部分) ->setName('app:create-user') // 配置一个参数 ->addArgument('username', Argument::REQUIRED, 'The username of the user.') // 运行 "php think list" 时的简短描述 ->setDescription('Creates new users.') // 运行命令时使用 "--help" 选项时的完整命令描述 ->setHelp("This command allows you to create users..."); } protected function execute(Input $input, Output $output) { // 输出多行到控制台(每一行的末尾添加 "\n") $output->writeln([ 'Create User', '============', '', ]); // 使用 getArgument() 取出参数值 $output->writeln('Username: ' . $input->getArgument('username')); } } ~~~ 现在,你可以传入用户名到命令中: ~~~ $ php think app:create-user hellokitty Create User ============ Username: hellokitty ~~~ ## 命令的生命周期 命令有三个生命周期方法可以在运行命令时使用: `initialize()`(可选) > 此方法在`interact()`和`execute()`方法之前执行。它的主要作用是初始化那些用在命令其余方法中的变量。 `interact()`(可选) > 此方法在`initialize()`之后、`execute()`之前执行。它的作用是检查是否错失了某些选项/参数,然后以互动方式向用户请求这些值。这是你可以问询错失的选项/参数的最后一个地方。此后,丢失的选项/参数将导致一个错误。 `execute()`(必须) > 此方法在`interact()`和`initialize()`之后执行。它包含你希望命令去执行的逻辑。