Magento 2 - Create Custom Command Line to Magento 2 CLI Console

I wrote these tutorials for myself in future when I forget for the next steps.
Search for a command to run...

I wrote these tutorials for myself in future when I forget for the next steps.
No comments yet. Be the first to comment.
When you provision an Ubuntu instance on Oracle Cloud, you'll notice something different from standard Ubuntu installations: the system uses iptables instead of ufw for firewall management. This disti

When developing web applications locally, HTTPS is often overlooked. Yet, modern browsers and APIs increasingly require secure connections—even in development. That’s where mkcert comes in: a simple t

Testing the checkout success page in Magento 2 can be frustrating. By default, once an order is placed, Magento clears the quote session, meaning you can’t simply refresh or revisit the success page without going through the entire checkout process a...

Magento Commerce (EE) offers powerful features, but for many teams, Open Source (CE) is leaner, lighter, and more sustainable. If you're migrating from EE to CE, the Opengento downgrade tool provides a clean, scriptable way to remove proprietary modu...

If you’ve ever checked your Apache error logs and seen something like this: Code [core:error] (13)Permission denied: access to /robots.txt denied (filesystem path '/home/username/sites') because search permissions are missing on a component of the pa...

I'm trying to create an article to create custom command line, in summary what we need to do are:
Define command on di.xml.
Create new Console Class.
Call the command
First we need to define the command on di.xml, this for simple example we're going to use in this article:
Filepath: app/code/Fiko/Magento/etc/di.xml
<?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="FikoWelcome" xsi:type="object">Fiko\Magento\Console\Welcome</item>
</argument>
</arguments>
</type>
</config>
Focus on item syntax, the rest are rule by magento in order to create custom command line.
name="FikoWelcome", it is the command name, it's the identity of this command.
xsi:type="object", the type of what's inside this item syntax, in this case it's object.
Fiko\Magento\Console\Welcome, magento 2 will call this class to be registered to magento as a custom command.
Then we need to create class for this custom command.
Filepath: app/code/Fiko/Magento/Console/Welcome.php
<?php
namespace Fiko\Magento\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Welcome extends Command
{
protected function configure()
{
$this->setName('fiko:welcome');
$this->setDescription('Demo description of welcoming someone!');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("Welcome Home!");
}
}
configure and execute are default methods of creating custom command.
configure is a method to define name or identity we need to type on command line.
execute is default method that will handle the action.
Finally, in order to call the command you have already created, you need to setup:di:compile and cache:clear.
bin/magento setup:di:compile
bin/magento cache:clear
Then you can call it by executing
bin/magento fiko:welcome
it's an optional and will be covered on next article, if you wish to add custom argument to your command line, you can visit this article.