How to add CLI commands

app/code/VendorName/TestModule/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <type name="Magento\Framework\Console\CommandList">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="exampleSayHello" xsi:type="object">VendorName\TestModule\Console\Sayhello</item>
            </argument>
        </arguments>
    </type>
</config>

app/code/VendorName/TestModule/Console/Sayhello.php

<?php
namespace VendorName\TestModule\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Sayhello extends Command
{
    protected function configure()
    {
        $this->setName('exampleGroup:exampleSubject:sayHello');
        $this->setDescription('Demo command line');

        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln("Hello World");
    }
}

php bin/magento

php bin/magento exampleGroup:exampleSubject:sayHello;

References

How to Add Command line in to Console CLI in Magento 2
Command naming guidelines

发表评论

电子邮件地址不会被公开。 必填项已用*标注