Magento2 has a large number of commands that make it really simple to manage but not only that, it also can create your very own commands, and this is awesome!.
A new Magento2 feature its console component.
The Root folder now contains a new directory – /bin, and there’s a ‘magento’ script inside, used to start the console component. By typing bin/magento on the terminal, we receive some commands we can run like the following:
- create an admin user (admin:user:create).
- clear, disable, enable cache (cache:clean, cache:enable, cache:disable, etc.).
- run cron (cron:run).
- enable and disable modules (module:enable, module:disable).
- check indexer status, and reindex if needed (indexer:info, indexer:reindex, etc.).
- and many more.
This is a major improvement when compared to Magento1, although this one was borrowed from Symfony, Cool Magento!.
This is a list of the most common Cli commands:
Setting up an upgrade using a command line
php bin/magento setup:upgrade
Cache clean using a command line
php bin/magento cache:clean
Cache flush using a command line
php bin/magento cache:flush
Checking cache status using a command line
php bin/magento cache:status
Enabling cache using a command line
php bin/magento cache:enable [cache_type]
Disabling cache using a command line
php bin/magento cache:disable [cache_type]
Deploying static content using a command line
php bin/magento setup:static-content:deploy
Deploying static content for a particular language using a command line
php bin/magento setup:static-content:deploy en_US
Reindexing using a command line
php bin/magento indexer:reindex
Displaying the indexers’ list using a command line
php bin/magento indexer:info
Displaying indexer’s status using a command line
php bin/magento indexer:status
Displaying all status’ modules using a command line
php bin/magento module:status
Enabling modules using a command line
php bin/magentomodule:enable Namespace_Module
Disabling modules using a command line
php bin/magento module:disable Namespace_Module
Uninstalling modules using a command line
php bin/magento module:uninstall Namespace_Module
Changing to developer mode using command line
php bin/magentodeploy:mode:setdeveloper
Changing to production mode using a command line
php bin/magentodeploy:mode:set production
Running the single-tenant compiler using command line
php bin/magentosetup:di:compile
Now, we’ll see how to add a new console command into magento2 console CLI.
Step 1: Defining a command on di.xml
File: app/code/Magmalabs/HelloWorld/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="magentoSayHello" xsi:type="object">Magmalabs\HelloWorld\Console\Sayhello</item>
</argument>
</arguments>
</type>
</config>
Step 2: Creating a command class
File: app/code/Magmalabs/HelloWorld/Console/Sayhello.php
namespace Magmalabs\HelloWorld\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Sayhello extends Command
{
protected function configure()
{
$this->setName('magento:sayhello');
$this->setDescription('Demo command line');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("Hello World");
}
}
- – configure() method is used to set the name and the description of the magento2 add command line.
- – execute() method will run when we call this command line via console.
And it’s done!
Flush Magento cache and type this command:
php magento --list
Our command will be shown here, run the command:
php bin/magento magento:sayHello
and the response is, as expected:
Hello World!
I hope this Magento2 technical note has helped you finding what you were looking for.