How to Create a Custom Console Command in Magento 2

Magento 2 out of the box supports a command-line interface (CLI). Using Magento 2 CLI you can perform multiple operations from installing Magento 2 software to installing Magento 2 module. There are many useful commands available to perform different tasks. However, sometimes you need a custom console command in Magento 2 that performs some complex data manipulation operation.

Today we will see how we can build a custom console command in Magento 2 module. We will highlight only the required files and not the whole module. We assume that the Magento 2 backend module is already created.

Create a Custom Console Command in Magento 2

Step 1: Create di.xml file under your module etc directory. Add below content in this file.

<?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="import" xsi:type="object">Codextblog\Demo\Console\Command\Import</item>
			</argument>
		</arguments>
	</type>
</config>

Step 2: Create Import.php class file under your module Console/Command directory. Add below code in this file.

<?php
namespace Codextblog\Demo\Console\Command;

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

class Import extends Command
{

    const NAME_ARGUMENT = "csv_name";
    
    /**
     * {@inheritdoc}
     */
    protected function execute(
        InputInterface $input,
        OutputInterface $output
    ) {
        $name = $input->getArgument(self::NAME_ARGUMENT);
        $output->writeln("Importing CSV " . $name);
    }

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this->setName("demo:import");
        $this->setDescription("Import data from csv file");
        $this->setDefinition([
            new InputArgument(self::NAME_ARGUMENT, InputArgument::OPTIONAL, "Csv Name")
        ]);
        parent::configure();
    }
}

Step 3: Command is ready. Run the below command in console.

php bin/magento demo:import test.csv

Once you run this console command, the execute method will run and print output Importing CSV test.csv. You can add your logic in the execute function. Please note that here we have defined one optional argument that is csv_name. As a result, we are passing the name of the csv file. However, If you do not require to pass the argument then you can remove the argument.

Want to ask a question or leave a comment?

Leave a Comment

(0 Comments)

All the comments are goes into moderation before approval. Please do not spam. Your email address will not be published. Required fields are marked *

Enjoy this post? Please support Us!

Follow us on twitter for daily new updates..

 

Want to become
a Magento 2 Expert?

If Yes! Then Subscribe to our newsletter and get weekly article to you email id.
Subscribe Here
SUBSCRIBE NOW
close-link

To Avoid Spam Downloads, We Want Your Email

We will send you download link right
away. Please submit form below.
SEND ME DOWNLOAD LINK
Close