thinkphp6---自定义指令

创建自定义指令操作步骤:

第一步:运行指令

php think make:command Auto auto

即可看到在 app\command 目录生成的 Auto.php 

修改里面的代码:

<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Auto extends Command
{
    protected function configure()
    {
        // 命令行参数
        $this->addArgument('a', Argument::REQUIRED); // 必须参数
        $this->addArgument('b', Argument::REQUIRED); // 必须参数
        $this->setName('auto');
        $this->setDescription('生成模块');
    }

    protected function execute(Input $input, Output $output)
    {
        $a = $input->getArgument('a');
        $b = $input->getArgument('b');
        echo $a.'+'.$b;
    }

}

第二步:修改 config/console.php 文件

<?php
return [
    'commands' => [
        'auto' => 'app\command\Auto',
    ]
];

第三步:测试

php think auto 100 200

结果:

 

posted @ 2021-12-31 10:36  帅到要去报警  阅读(476)  评论(0编辑  收藏  举报