How to Create a Custom Console Command in Magento 2

As we all know by default Magento 2 provides a list of commands which we'll regularly use. But, in some cases, we'll need to create some custom commands to fulfill our other requirements. So, this article is all about that.

Let's learn how to create a custom command in Magento 2?

You need to follow some simple steps to create new Magento 2 CLI command.

Before we start I assume, you have already a created custom module. If you don't have it or don't know how to create it then check out our other article How To Create a Magento 2 Module.

1. Define Command in di.xml file

Create a di.xml file at app/code/Devhooks/HelloWorld/etc/di.xml and paste the below code.

Copy
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
   <type name="Magento\Framework\Console\CommandList">
       <arguments>
           <argument name="commands" xsi:type="array">
               <item name="Customcommand" xsi:type="object">Devhooks\HelloWorld\Console\Customcommand</item>
           </argument>
       </arguments>
   </type>
</config>

2. Define Custom Command Class

Create a Customcommand.php class file at app/code/Devhooks/HelloWorld/Console/Customcommand.php and paste the below code.

Copy
<?php

namespace Devhooks\HelloWorld\Console;

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

class Customcommand extends Command
{
    const NAME = 'name';

    protected function configure()
    {
        $this->setName('devhooks:first:command');
        $this->setDescription('This is devhooks first console command.');
        $this->addOption(
            self::NAME,
            null,
            InputOption::VALUE_REQUIRED,
            'Name'
        );

        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        if ($name = $input->getOption(self::NAME)) {
            $output->writeln('<info>Provided name is `' . $name . '`</info>');
        }

        $output->writeln('<info>Success Message.</info>');
        $output->writeln('<error>An error encountered.</error>');
        $output->writeln('<comment>Some Comment.</comment>');
    }
}

In above code in configure() function we have define new command name, description and it's option. Here addOption is a optional if you required any parameter with your command then you can use it.

Here we have use name as a required parameter. If you don't want to use name as require parameter then you just need to remove InputOption::VALUE_REQUIRED, line from the addOption.

Also, we have define execute() function in above code and it'll execute when you run devhooks:first:command in command line.

Bingo!! we are done, now open command line and move to root folder of magento and run below command and flush magento cache.

Copy
php bin/magento c:f

And run below command to check the Magento command list and find your command in a list.

Copy
php bin/magento

Also, you can run your command with option like below

Copy
php bin/magento devhooks:first:command --name "Dev hooks"

That's it!!

If you are a beginner and want to know more about Magento 2 other Commands then check out our other article which is Useful Commands for Magento 2.

I hope this article helped you to find what you were looking for.

Bookmark it for your future reference. Do comment below if you have any other questions on that.

P.S. Do share this article with your team.




Search
Recent Posts
    Tags
    Newsletter
    SocialFollow on Twitter