Skip to content

Instantly share code, notes, and snippets.

@alexmorco
Last active April 12, 2018 11:49
Show Gist options
  • Save alexmorco/863eafa2a83e735168b8d80811d4945c to your computer and use it in GitHub Desktop.
Save alexmorco/863eafa2a83e735168b8d80811d4945c to your computer and use it in GitHub Desktop.
Add Custom CLI Commands In Magento 2

The new command line interface (CLI) enables Magento developers to use default command provided by Magento modules. Using this, developers could manage modules, carry our database updates, manage indexes and generate classes.

The great thing is that Magento developers could now add custom CLI commands to their modules. While this is great, developers should always make sure that they observe the best code writing standards as defined by the Magento dev community.

Add a CLI Command

The process of adding a new CLI command is simple. In this tutorial, I will not go into the details of creating a Magento 2 module.

Just create a simple module Cloudways_Commandline with etc/module.xml and registration.php files included in the root of module’s directory.

Now, create a di.xml file in the etc directory of your module. Insert the following code in the 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="CloudwaysHicustomer" xsi:type="object">Cloudways\Commandline\Console\Hicustomer</item>

           </argument>

       </arguments>

   </type>

</config>

As you can see, I have added an entry in di.xml file. The tag is pointing to Magento\Framework\Console\CommandList class. Furthermore, it has the named commands, and the tag. It seems like Magento 2 is doing some magic by sending the argument to CommandList class. That is how dependency injection works – injecting the Command object (defined in the tag) into an array of all commands.

I have also declared a class Cloudways\Commandline\Console\Hicustomer in the tag. This class will set the name, and description of command using the configure() method. The class will also define the execute() method for the command.

The next step is the creation of the class file Hicustomer.php (as defined in di.xml), in the Console directory of the module and add the following code to it:


<?php

namespace Cloudways\Commandline\Console;

 

use Symfony\Component\Console\Command\Command;

use Symfony\Component\Console\Input\InputInterface;

use Symfony\Component\Console\Output\OutputInterface;

 

class Hicustomer extends Command

{

   protected function configure()

   {

       $this->setName('cloudways:hicustomer');

       $this->setDescription('Demo command line');

      

       parent::configure();

   }

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

   {

       $output->writeln("Hi Customer");

   }

}

The Hicustomer class has two methods. The configure() method is used to set initial configuration (such as name, description, and arguments) for the command. The execute() method will contain the logic that would be executed when the CLI command is called in the console.

Once the class has been declared, enable the module, run the setup upgrade command and flush Magento 2 cache. Now, type the following CLI command to see the list of all available Magento 2 commands.


php bin/magento list

You will notice that the custom CLI command is also on the list.

Now, run the php bin/magento cloudways:hicustomer command to see the desired result.

This was just a basic CLI command. To make things interesting, I will now demonstrate how to add an argument to the custom command. For this, replace the current contents of the class file Hicustomer.php with the following.


<?php

namespace Cloudways\Commandline\Console;

 

use Symfony\Component\Console\Command\Command;

use Symfony\Component\Console\Input\InputInterface;

use Symfony\Component\Console\Output\OutputInterface;

use Symfony\Component\Console\Input\InputOption;

 

class Hicustomer extends Command

{

   const CUSTOMERNAME = 'Name';

 

   protected function configure()

   {

       $commandoptions = [new InputOption(self::CUSTOMERNAME, null, InputOption::VALUE_REQUIRED, 'Name')];

 

       $this->setName('cloudways:hicustomer');

       $this->setDescription('Demo command line');

       $this->setDefinition($commandoptions);

      

       parent::configure();

   }

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

   {

       if ($customername = $input->getOption(self::CUSTOMERNAME))

       {

         $output->writeln("Hi ".$customername);

       }

       else

       {

         $output->writeln("Hi Customer");

       }

   }

}

As you can see, I have created and added the Name argument for custom command in configure() method using $this->setDefinition($commandoptions); function and get it in execute() method.

Once again, run setup upgrade command and flush Magento 2 cache. Invoke the custom CLI command by php bin/magento cloudways:hicustomer --Name="Fayyaz" and it will show “Hi Fayyaz” as an output.

Note: This article was originally published at Cloudways. You can check more about Adding Custom CLI Commands In Magento 2 Version at: https://www.cloudways.com/blog/custom-cli-command-magento-2/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment