As you know, Magento 2 Open Source is a modern PHP-based eсommerce framework.
And as with every framework, it provides a command-line interface for performing different operations with the system.
It includes
- the installation of a website
- enabling additional plugins
- modifying data
- performing maintenance activities such as caching security performance-related commands
- and much more…
Magento 2 Open Source includes a so-called Magento CLI that stands for Magento Command Line Interface that ships together with Magento 2 Open Source.
Following is a summary of the process
- Create a Command class (the recommended location is <your component root dir>/Console/Command). See <Magento_Store_module_dir>/Console/Command/StoreListCommand.php for example.
<?php
namespace Magento\CommandExample\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class SomeCommand
*/
class SomeCommand extends Command
{
private const NAME = 'name';
/**
* @inheritDoc
*/
protected function configure()
{
$this->setName('my:first:command');
$this->setDescription('This is my first console command.');
$this->addOption(
self::NAME,
null,
InputOption::VALUE_REQUIRED,
'Name'
);
parent::configure();
}
/**
* Execute the command
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return null|int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($name = $input->getOption(self::NAME)) {
$output->writeln('<info>Provided name is `' . $name . '`</info>');
}
$output->writeln('<info>Success Message.</info>');
$output->writeln('<error>An error encountered.</error>');
$output->writeln('<comment>Some Comment.</comment>');
}
}
2. Declare your Command class in Magento\Framework\Console\CommandListInterface and configure the command name using dependency injection (<your component root dir>/etc/di.xml):
<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="commandexample_somecommand" xsi:type="object">Magento\CommandExample\Console\Command\SomeCommand</item>
</argument>
</arguments>
</type>
...
</config>
3. Clean the cache:
$ bin/magento cache:clean
4. Regenerate the code:
$ bin/magento setup:di:compile
As a result, the new command my:first:command that accepts a –name parameter is ready to use.
$ bin/magento my:first:command –name ‘John’
Congratulations! You’re the winner!
But there are other tasks for CLI. Also you should know about:
- Changing Magento 2 CLI File Permissions
- The CLI commands difference between installed and non-installed Magento 2
- The Admin User Create command
- The Admin URI Command
- The Admin User Unlock Command
- The deploy:mode:show Command
Good news! You can learn it for free!
Mage Mastery has a free course: Magento 2 Command Line Interface (CLI) for Beginners.
What about conditions?
Absolutely Free
The course materials are absolutely free and you have 24/7 access to watch videos on your own pace.
Knowledge Gain
Each lesson content has been carefully polished and recorded so you can get most out with Mage Mastery