This Article is about how to create a custom console command in Magento 2. In Magento 2, a list of available commands is used to register a module, clear the cache, install/upgrade database schema, manage indexing/reindexing and run a Magento cron job over a fixed period of time. Magento developers are pretty familiar with these commands but sometimes they need to create custom commands to handle complex situations, for this Magento provides the functionality where Magento developers can use their own custom commands.
Step 1: Create file registration.php at app/code/Vendor/Module/ folder (In my case vendor is Bluethink, Module is CustomCommand and my file path is: app/code/Bluethink/CustomCommand/registration.php)
1 2 3 4 5 6 7 8 9 10 |
<?php /** * Copyright © Bluethink Inc. All rights reserved. */ \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Bluethink_CustomCommand', __DIR__ ); |
Step 2: Create module.xml file at app/code/Vendor/Module/etc/ folder (In my case vendor is Bluethink, Module is CustomCommand and my file path is : app/code/Bluethink/CustomCommand/etc/module.xml)
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0"?> <!-- /** * Copyright © Bluethink Inc. All rights reserved. */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Bluethink_CustomCommand" > </module> </config> |
Step 3:Create di.xml file at app/code/Vendor/Module/etc folder (In my case my file path is : app/code/Bluethink/CustomCommand/etc/di.xml)
1 2 3 4 5 6 7 8 9 10 11 |
<?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="Mycommand" xsi:type="object">Bluethink\CustomCommand\Console\ Command\Mycommand</item> </argument> </arguments> </type> </config> |
Step 4:Create Mycommand.php file at app/code/Vendor/Module/Console/Command folder (In my case my file path is : app/code/Bluethink/CustomCommand/Console/Command/Mycommand.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php namespace Bluethink\CustomCommand\Console\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class Mycommand extends Command { protected function execute(InputInterface $input, OutputInterface $output) { system("rm -rf var/cache/* var/page_cache/* pub/static/frontend/* var/view_preprocessed/pub/static/frontend/*"); $output->writeln("<info>cache, page_cache, view_preprocessed, pub/static/frontend all cache deleted successfully</info>"); } protected function configure() { $this->setName('removecache:all'); $this->setDescription('Delete cache, page_cache, view_preprocessed by custom command'); parent::configure(); } } |
Note:If you are not using an existing module then run the appropriate commands to register your module and dependencies using –
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush
Now check your custom command is appearing in Command List by using –
php bin/magento
you will find removecache:all command in command list.
Finally, Run the following command php bin/magento removecache:all
bluethinkinc_blog
2022-12-06