php脚本自定义指令,自定义指令

本文介绍了在ThinkPHP5.1框架中如何创建和使用自定义命令,包括定义命令类、配置命令、测试命令以及在控制器中调用命令的方法。还提到了快速生成指令的快捷方式以及如何在命令行和控制器中运行这些自定义指令。
摘要由CSDN通过智能技术生成

# 自定义指令

## 创建自定义指令

第一步,创建一个自定义命令类文件,新建`application/common/command/Hello.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`文件

```

return [

'app\common\command\Hello',

];

```

`V5.1.24+`版本开始,你可以定义为下面的方式提高性能。

```

return [

// 指令名 =》完整的类名

'hello'=>'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

```

> 注意看参数和选项的调用区别

## 在控制器中调用命令

支持在控制器的操作方法中直接调用命令,例如:

```

namespace app\index\controller;

use think\Console;

use think\Controller;

class Index extends Controller

{

public function hello($name)

{

$output = Console::call('hello ' . $name);

return $output->fetch();

}

}

```

访问该操作方法后,例如:

```

http://tp5.com/index/index/hello/name/thinkphp

```

页面会输出

```

Hello thinkphp!

```

> 一旦在控制器中调用命令,并不会生成cli命令行日志,而是在普通的web日志中记录。

## 快速生成指令(`V5.1.24+`)

`V5.1.24+`版本开始,你可以通过命令行指令快速生成一条指令,包括指令类文件,例如:

```

php think make:command First first

```

`First`表示类名,实际生成的指令类则是`app\command\First`(自动生成的指令类的命名空间默认都是 `app\command`)。

`first`表示指令名,建议统一使用小写,如果不传入该参数,则默认用类名的小写作为指令名。

为了让指令可以无需定义自动调用,需要在你的应用配置的`console.php`配置文件中,增加下面的配置参数:

```

'auto_path' => env('app_path') . 'command/',

```

配置后, 你可以在命令行测试下刚才新生成的指令。

```

php think first

```

输出结果为:

```

first

```

你可以编辑`app\command\First`类,完成实际的指令任务。

> 注意,如果你生成了指定命名空间的指令,但又不是你配置的自动加载目录,那么仍然需要在`command.php` 文件中定义指令。

如果需要生成一个指定的命名空间,可以使用:

```

php think make:command app\index\Second second

```

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值