Creating custom console commands can be very useful in Magento 2, especially for developers. The old fashion way in Magento 1 was to create external scripts in various folders or, the better way, in "shell/", however, if you had multiple "commands" you ended up having a lot of files.
Magento 2 came up with a much nicer and easier solution, and also much more organized. You can now create custom console commands directly in your extension and execute them using the bin/magento command.
There are two important things to do to successfully create a custom command:
1. Define your command in your di.xml situated in "etc" folder of your extension. You will need to create it if it doesn't exist yet
<?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="test" xsi:type="object">DEVLINE\Testimonial\Console\Command\TestCommand</item>
</argument>
</arguments>
</type>
</config>
app/code/DEVLINE/Testimonial/etc/di.xml
2. After you've defined your command, you will also need to create the class for it. Recommended is to create it starting with folder Console/Command/ in your extension.
<?php
namespace DEVLINE\Testimonial\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class TestCommand extends Command
{
protected function configure()
{
$this->setName('devline:test');
$this->setDescription('Will print common message types.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('This is a normal message.');
$output->writeln("<info>This is an info message.</info>");
$output->writeln("<comment>This is a comment.</comment>");
$output->writeln("<error>This is an error message.</error>");
}
}
app/code/DEVLINE/Testimonial/Console/Command/TestCommand.php configure method is used to set Name, Description or other configurations of your command
execute method is used to do what you want in the command, here you will write all your processes that need to be executed
Before you can test your command, it is recommended to clear the caches in Magento by running the following commands in your terminal:
rm -rf var/cache/*
rm -rf var/page_cache/*
rm -rf var/generation/*
rm -rf var/di/*
Now that you've done all the necessary steps, you can first run next command to see that Magento knows about your command:
If your command appears in that list, it means that everything went well and you can finally run your command and see its result.
In my example I will run:
php bin/magento devline:test
As I wrote in my execute method, it will list the most common message types used in console commands that you will or can use as well based on your needs.
Hopefully, this article helped you and other developers to stay more organized with this new possibility in Magento 2.
Nice article, I got help while working on Magento 2 CLI. I have done with following: Cache Cron Setup (Backup, Config) and for Admin I also got help from this post, https://www.cloudways.com/blog/magento-2-cli/, hope it will help your readers as well.