How to include new command line to Magento 2

In this article, I will show you how to include new command line to Magento 2 CLI (php bin/Magento).

Firstly, construct 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=" Webnexs_cli" xsi:type="object">Webnexs\Console\Console\Command\CustomCommand</item>

 </argument>

 </arguments>

 </type>

 </config>

Next, please create CustomCommand.php

File in Webnexs \Console\Console\Command folder:

<?php

namespace Webnexs\Console\Console\Command;


use Symfony\Component\Console\Output\OutputInterface;

use Symfony\Component\Console\Input\InputInterface;


class CustomCommand extends Symfony\Component\Console\Command\Command


{


    protected function configure()

    {

        $this->setName('Webnexs:Webnexs_cli)->setDescription('Webnexs Custom Command.');

    }


    protected function execute(InputInterface $input, OutputInterface $output)

    {

        $output->writeln('First Command!');

    }

}

To verify it, please type the following command line:

php bin/Magento Webnexs:Webnexs_cli


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *