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

第一步:运行指令

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

结果:

thinkphp6---自定义指令

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-11-28
  • 2021-11-28
  • 2021-10-14
  • 2021-08-31
  • 2021-09-13
猜你喜欢
  • 2021-11-28
  • 2021-06-29
相关资源
相似解决方案