How To

How to Call Function from CLI Command in Magento 2

Hello Magento Friends,

Today I am going to explain How to Call Function from CLI Command in Magento 2.

You can also call a function using a block file from the CLI command to Magento 2. Check out the below steps to learn how to accomplish it.

Steps to Call Function from CLI Command in Magento 2:

Step 1: Firstly, you need to run the below command

php bin/magento example:hicustomer

Step 2: You need to call the block file by creating a di.xml file at the below path.

app\code\Vendor\Extension\etc\di.xml

And add the code as mentioned below

<?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="hicustomer" xsi:type="object">Vendor\Extension\Console\Hicustomer</item>
           </argument>
       </arguments>
   </type>
</config>

Step 3: Now create a file at the following path

app\code\Vendor\Extension\Console\Hicustomer.php

And add the below code

<?php
namespace Vendor\Extension\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Hicustomer extends Command
{
   protected function configure()
   {
       $this->setName('example:hicustomer');
       $this->setDescription('Demo command line');
       
       parent::configure();
   }
   protected function execute(InputInterface $input, OutputInterface $output)
   {
       $output->writeln("Hello World");
   }
}

Conclusion:

Hence, this way you can Call Function from CLI Command in Magento 2. Check out the list of useful CLI commands for Magento 2. Share your queries via the comment section. Let your friends know about this solution. Stay tuned for the next one!

Happy Coding!

Click to rate this post!
[Total: 2 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

3 hours ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

1 week ago