SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH

How create a simple console command extension in Magento 2

How create a simple console command extension in Magento 2
How create a simple console command extension in Magento 2

In Magento 2, You probably familiar with command lines:

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy -f

php bin/magento cache:flush

How do you create a command line? How can you create a custom command line for your Magento project?

In this article, I will guide you on how to create an extension with console command in Magento 2.

Step 1: Create registration.php file in path of module app/code/Magepow/HelloCli/registration.php.

Content would be:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    ‘Magepow_HelloCli’,
    __DIR__
);

Step 2: Create di.xml file in path of module app/code/Magepow/HelloCli/etc/di.xml.

Content would be:

<?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=“HelloCli” xsi:type=“object”>Magepow\HelloCli\Console\HelloCli</item>
           </argument>
       </arguments>
   </type>
</config>

Step 3: Create module.xml file in path of module app/code/Magepow/HelloCli/etc/module.xml.

Content would be:

<?xml version=“1.0”?>
<config xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=“urn:magento:framework:Module/etc/module.xsd”>
    <module name=“Magepow_HelloCli” setup_version=“0.1.0”/>
</config>

Step 4: Create HelloCli.php file in path of module app/code/Magepow/HelloCli/Console/HelloCli.php.

Content would be:

<?php
namespace Magepow\HelloCli\Console;

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

class HelloCli extends Command
{
   protected function configure()
   {
       $this->setName(‘magepow:sayhello’);
       $this->setDescription(‘Magepow command line’);
       
       parent::configure();
   }
   protected function execute(InputInterface $inputOutputInterface $output)
   {
       $output->writeln(“Magepow Hello World”);
   }
}

Done! Now, you can check it by command line:

php bin/magento magepow:sayhello