Home >How to create console command in Magento 2

How to create console command in Magento 2

In Magento 2, you are probably familiar with commands: php bin/magento cache, php bin/magento setup:upgrade, php bin/magento setup:static-content:deploy…how are these commands created? And how can you create a custom command for your own project?

create_console_command_m2

AD

M2D

In Magento 2, you are probably familiar with commands such as:

  • php bin/magento cache:flush
  • php bin/magento setup:upgrade
  • php bin/magento setup:static-content:deploy…

But how are these commands created? And how can you create a custom command for your own project? In this article, we will give you a detailed instruction to create console command in Magento 2 as the following steps.

Step 1: Create a new module for your project. We will use an example module Bss_CheckCli to demo for the lesson.

Step 2: Create di.xml file in path of module app/code/Bss/CheckCli/etc/di.xml and put this in.

<?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\CommandListInterface">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="bss_check_command" xsi:type="object">Bss\CheckCli\Console\Command\CheckCli</item>
            </argument>
        </arguments>
    </type>
</config>

Step 3: Create CheckCli.php file in path of module app/code/Bss/CheckCli/Console/Command/CheckCli.php.

In this step, there are 2 methods you can use: execute the logic after running the command and execute the logic after passing a parameter.

  • To Execute the logic after running the command, you should refer the following code:
<?php
namespace Bss\CheckCli\Console\Command;
use Magento\Framework\App\State as AppState;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CheckCli extends \Symfony\Component\Console\Command\Command
{
    protected $appState;
    public function __construct(
        AppState $appState
    ) {
        $this->appState = $appState;
        parent::__construct();
    }

    protected function configure()
    {
        $this->setName('bss:checkcli:custom') //  Your command name
            ->setDescription(‘This is Custom CLI command.'); // description for your command cli
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->appState->setAreaCode('adminhtml');
	// Your logic here.
        $output-> writeln(‘Check Command CLI successfully’);  // print message to terminal
    }
}
  • To Execute the logic after passing a parameter, you can check the code as below:
<?php
namespace Bss\CheckCli\Console\Command;
use Magento\Framework\App\State as AppState;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
class CheckCli extends \Symfony\Component\Console\Command\Command
{
    protected $appState;
    public function __construct(
        AppState $appState
    ) {
        $this->appState = $appState;
        parent::__construct();
    }
   protected function configure()
    {
        $this->setName('bss:checkcli:custom') //  Your command name
            ->setDescription('Check Command CLI successfully'); // description for your command cli
        $this->addArgument( ‘yourparam’,  InputArgument::REQUIRED,  'Your param EX: test' ); // Add your param to terminal
    }
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $yourParam = $input->getArgument(‘yourparam’); // Get your param from terminal for execure your logic.
        // Your logic here
          $output-> writeln(‘Check Command CLI with Param successfully’);  // print message to terminal
    }
}

The result

After you have done these above steps, let’s go to SSH console and run command line: php bin/magento bss:checkcli:custom.

< Previous Post
Next Post >
+ posts