控制台php脚本,控制台命令 · ThinkPHP5命令行实例详解 · 看云

# 控制台命令

[TOC]

## 创建一个命令

> 命令通过类来定义,一个合法的命令类,没有固定的目录和命名空间要求,但必须继承 `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)中执行它:

```bash

$ 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.');

}

}

```

现在,尝试执行此命令:

```bash

$ 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'));

}

}

```

现在,你可以传入用户名到命令中:

```bash

$ php think app:create-user hellokitty

Create User

============

Username: hellokitty

```

## 命令的生命周期

命令有三个生命周期方法可以在运行命令时使用:

`initialize()` (可选)

> 此方法在 `interact()` 和 `execute()` 方法之前执行。它的主要作用是初始化那些用在命令其余方法中的变量。

`interact()` (可选)

> 此方法在 `initialize()` 之后、 `execute()` 之前执行。它的作用是检查是否错失了某些选项/参数,然后以互动方式向用户请求这些值。这是你可以问询错失的选项/参数的最后一个地方。此后,丢失的选项/参数将导致一个错误。

`execute()` (必须)

> 此方法在 `interact()` 和 `initialize()` 之后执行。它包含你希望命令去执行的逻辑。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值