Magento, Magento 2

Create new command in console CLI in Magento 2

Create new console command in CLI in Magento 2

In this article, we will see about how to create new command in console CLI in Magento 2. Magento 2 provides many console CLI commands to perform action very quickly as like :

  • Install Magento (and related tasks such as creating or updating 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

In Magento 1, there are no console command line interface (CLI) command provide. Therefore, It’s major plus point that Magento 2 provide console CLI command.

Similarly, If you want to check command list of Magento 2, then you can check here.

By create custom console CLI command developer can manage module, actions, indexing etc. very quickly. So, Let’s start create new command in console CLI. In addition, If you want to create simple module, you can check here.

Step 1: Define command in di.xml

File : app/code/RH/Helloworld/etc/di.xml

In di.xml file, We need to use Magento\Framework\Console\CommandList to define the command option.

<?xml version="1.0"?>
<!--
/**
 * Created By : Rohan Hapani
 */
-->
<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">RH\Helloworld\Model\Generation</item>
            </argument>
        </arguments>
    </type>
</config>

RH\Helloworld\Model\Generation class will define the command name and execute() method for this command.

Step 2: Create command class file

File : app/code/RH/Helloworld/Model/Generation.php

<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\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.');
    }
}

In this file, There are define 2 methods

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

If you want to check your command will be show in command list or not, you can execute this command :

You can see your new command after install this module.

Finally, execute php bin/magento generation:clean command. It will execute rm -r generated/* command & clear generated folder.

 

Tagged , ,