导航

TP6:第一个Command

Posted on 2022-05-10 11:17  eastson  阅读(649)  评论(0编辑  收藏  举报

ThinkPHP提供自定义命令功能,参考手册

如何自定义TP6的命令,参考thinkphp6 command(自定义指令)

1. 建立app/test/command/Hello.php。

<?php
declare (strict_types = 1);

namespace app\test\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')
            ->setDescription('the hello command');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $output->writeln('Hello ' . date('Y-m-d H:i:s'));
    }
}

2. 配置config\console.php。

<?php

return [
    'commands' => [
        'app\test\command\Hello',
    ],
];

3. 测试命令执行结果。