We use the command line while developing with Magento 2. For example, From installation to deployment, we use several commands daily.
We also can create our own custom console commands in Magento 2. We can write code that can be executed from the terminal.
As we know, Shell commands are faster than GUI-based applications. Therefore, we can use a console script in the activity in which we don’t require user engagement & require a good amount of operation.
Magento 2: How to create a console command?
We must create di.xml to pass an argument of the class object to the core class Magento\Framework\Console\CommandList.
We need to extend the class Symfony\Component\Console\Command\Command in our custom console class.
The configure function of our custom console class is to provide important information like command name, description and argument list to the console command. The configure function’s type is protected.
The execute() function of the console class is responsible for executing our console command. Therefore, we need to write the logic of our task in this function.
The type of the execute function is protected. This function contains two parameters: Symfony\Component\Console\Input\InputInterface and Symfony\Component\Console\Output\OutputInterface
Let’s check the practicality, of how we can create a console command in Magento 2 (Adobe Commerce).
I have defined di.xml under the etc directory of my module and PHP class for that console command inside the Console directory of my module.
I have added a command to reset the index in the given example.
<?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="ResetIndexer" xsi:type="object">SbDevBlog\Indexer\Console\ResetIndex</item>
</argument>
</arguments>
</type>
</config>
<?php
namespace SbDevBlog\Indexer\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;
use SbDevBlog\Indexer\Services\indexerService;
class ResetIndex extends Command
{
private const INDEXER_KEY = "indexerid";
/**
* @var indexerService
*/
private indexerService $indexerService;
public function __construct(
indexerService $indexerService,
string $name = null
)
{
$this->indexerService = $indexerService;
parent::__construct($name);
}
/**
* Configure command
*
* @return void
*/
protected function configure()
{
$this->setName("sbdevblog:resetindex")
->addArgument(self::INDEXER_KEY, InputOption::VALUE_REQUIRED, "Indexer Key")
->setDescription("Reset Index Based On Index Id");
parent::configure();
}
/**
* Resetting index by key
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$indexerId = $input->getArgument(self::INDEXER_KEY);
if ($this->indexerService->invalidateIndexById($indexerId)) {
$output->writeln(__("Invalidated %1 successfully", $indexerId));
return;
}
$output->writeln(__("Something is wrong, Please check Indexer Id %1", $indexerId));
}
}
The addArgument() function allows us to take an argument from the command line. For example, just run the command php bin/magento sbdevblog:resetindex with an argument of index id.
Example:
php bin/magento sbdevblog:resetindex catalog_category_product
O/P: Invalidated catalog_category_product successfully
Thanks for reading my blog. Please use the comment section to give your feedback.
To run re-index programmatically, Checkout the post: https://sbdevblog.com/magento-2-how-to-re-index-programmatically/
Note: Please verify the code of this blog and the relevant git repository before using it in production.
🙂 HAPPY CODING 🙂
Thank you it helped a lot