How to Create New Command in Console CLI in Magento 2?

Have you ever wanted to create a new PHP script or Magento shell for performing custom actions? Earlier, Magento came with /shell folder and around 50 PHP scripts and shell commands, which usually gave a headache to new Magento developers.

Magento 2, however, now comes with a new integrated console component. It’s highly extendable and customizable, which allows Magento developers to add custom commands easily.

This is, in fact, a major improvement over Magento 1. And in this tutorial, we’re going to teach you how to create a new command in Console CLI in Magento 2.

How to Add New Console Command in Magento 2

In Magento 1, as we mentioned earlier, there was no console CLI (Command Line Interface). But since Magento 2, it is being provided and you can also create your own custom console CLI command to manage actions, indexes, modules, etc.

Now, in order to add a new command in Console CLI in Magento 2, you need to first perform the following actions:

  • Install Magento (and also perform basic tasks such as creating the database schema, creating the deployment configuration, and so on)
  • Clean Cache
  • Manage indexing-reindexing
  • Upgrade and deploy process
  • Creating translation dictionaries and translation packages
  • Deploying static view files
  • Creating CSS from LESS

Once everything is done, we can get started with the process of creating a new command CLI. First, you’ll need to create a simple module in Magento 2. We have already written a tutorial on How to Create a Simple Module in Magento 2.

Step #1 - Define Command in di.xml

Once you’ve created a simple module, you will need to define command in di.xml file.

File path - app/code/MD/Helloworld/etc/di.xml

In the file, you need to use Magento\Framework\Console\CommandList to define your command option using the following code.

<?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="clean_generation" xsi:type="object">MD\Helloworld\Model\Generation</item>
            </argument>
        </arguments>
    </type>
</config>

Now, go to MD\Helloworld\Model\Generation class and define the command name and execute() method for your new command.

Step #2 - Create Command Class File

File path - app/code/MD/Helloworld/Model/Generation.php

Please copy the following code into the file.

<?php
namespace MD\Helloworld\Model;
use \Symfony\Component\Console\Command\Command;
use \Symfony\Component\Console\Input\InputInterface;
use \Symfony\Component\Console\Output\OutputInterface;
class Generation extends Command
{
    protected function configure()
    {
        $this->setName('generation:clean')->setDescription('Clean Generation Folder');
        parent::configure();
    }
    protected function execute(InputInterface $input, OutputInterface $output)
{   
        system("rm -r generated/*");
        $output->writeln('Generation Folder Clean Successfully.');
    }
}

Now, in this generation.php file, there are a total of two defined methods.

  • configure() - It’s used to set the name, description, command-line arguments of the Magento 2 add command line.
  • execute() - It’s used to run when we call this command line using the console.

Next, we need to check if your new command is shown in the command list or not. And for that, you need to execute the following command:

php bin/magento

As you run the command, you’ll see that your new command is listed after installing the module.

Lastly, you need to execute the command:

php bin/magento generation:clean

It will execute the below command & clear generated folder.

rm -r generated/*

Final Words…

In this tutorial, we covered exactly how you can create a new command in Console CLI in Magento 2.

We hope you found this tutorial helpful. And if you get stuck at any step or simply need our professional help, feel free to contact us anytime.