How to Create Console Command in Magento 2

How to Create Console Command in Magento 2

There are certain tasks that you do in a Magento 2 store that are mundane and repetitive and, unfortunately, they’re also often the most important. For example, running a custom cron after a fixed time period, or remove a directory after caching.

However, you can turn many of these boring processes into one-command affairs by using the below method to create console command in Magento 2.

The custom console commands in Magento 2 are very helpful when you want to perform tasks that are repetitive and are to be done in the same manner every time!

Here, I’ve taken a simple example to display a message when a custom Magento 2 CLI command is run:

Steps to Create Console Command in Magento 2:

  1. Create registration.php file in app\code\[Vendor]\[Namespace]\
  2. Create module.xml file in app\code\[Vendor]\[Namespace]\etc
  3. Create di.xml file in app\code\[Vendor]\[Namespace]\etc
  4. Create Ping.php in [Vendor]\[Namespace]\Model

Run following command:

The output message will be “Need Help Contact To Meetanshi.com”

Similarly, you can use the function execute() to create console command in Magento 2 as per your requirements.

Please use the Comments section below to mention any doubts and I’d be happy to help 🙂

Thanks.

 

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

5
(based on 8 Reviews)

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

Leave a reply:

Your email address will not be published.