How to Create Custom Command in Console CLI in Magento 2.
Magento 2, it is being provided default and you create your own custom console CLI command to manage actions, indexes, modules, etc. The new interface performs multiple tasks, including:
Also Like Read: Magento 2 Add Product to cart with a custom price
Custom console command in cli .Taken Dolphin as Package name and CustomCli as Modulename for simplicity. code snippets are under a module named Dolphin_CustomCli.
Before creating the customer tab, we need to create a new module We already have a blog on “how to create a basic module in Magento 2“. We need to create module.xml and registration.php file. You can create module.xml and registration.php from this tutorial.
File: app/code/Dolphin/CustomCli/etc/di.xml
you need to use Magento\Framework\Console\CommandList to define your command option.
<?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="Hellocustom" xsi:type="object">Dolphin\CustomCli\Console\Command\Hellocustom</item> </argument> </arguments> </type> </config>
This di.xml declare a command class Hellocustom (Dolphin\CustomCli\Console\Command\Hellocustom). This class defines the command name and execute() method.
File: app/code/Dolphin/CustomCli/Console/Command/Hellocustom.php
<?php namespace Dolphin\CustomCli\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 Hellocustom extends Command { const NAME_ARGUMENT = "name"; const NAME_OPTION = "option"; /** * {@inheritdoc} */ protected function execute( InputInterface $input, OutputInterface $output ) { $name = $input->getArgument(self::NAME_ARGUMENT); $option = $input->getOption(self::NAME_OPTION); $output->writeln("Hello " . $name); } /** * {@inheritdoc} */ protected function configure() { $this->setName("dolphin_customcli:hellocustom"); $this->setDescription("custom cli command"); $this->setDefinition([ new InputArgument(self::NAME_ARGUMENT, InputArgument::OPTIONAL, "Name"), new InputOption(self::NAME_OPTION, "-a", InputOption::VALUE_NONE, "Option functionality") ]); parent::configure(); } }
Next, we need to check your new command is show in command list.
Magento 2 Below CLI command to see the list of all available Magento 2 commands.
[dt_highlight color=”” text_color=”” bg_color=””]php bin/magento list[/dt_highlight]
Now you can run [dt_highlight color=”” text_color=”” bg_color=””]php bin/magento dolphin_customcli:hellocustom[/dt_highlight] from the command to see the result:
The CLI command by [dt_highlight color=”” text_color=”” bg_color=””]php bin/magento dolphin_customcli:hellocustom -a ‘jigar’[/dt_highlight] and it will show “Hello jigar” as an output.
I hope this instruction will be helpful for you.
If you have any difficulties regarding this blog, do consider them posting in the Comments section below!
Thank you!
Click one of our contacts below to chat on WhatsApp