CommercePeer Brand Logo


Programmatically: Magento 2 Product Entity

Programmatically: How to create a custom CLI command in Magento

Step 1: Create a new module

Create a new directory CustomCli inside the app/code/local directory. Inside this directory, create the following files: CustomCli/etc/module.xml:


		
	
    
        
            1.0.0
        
    
	
	

CustomCli/etc/config.xml:


		
	
    
        
            true
            local
        
    
	
	

Step 2: Create the CLI command

Create a new file CustomCli/Model/Command.php:


	use Magento\Framework\Setup\Template;
	use Symfony\Component\Console\Command\Command;
	use Symfony\Component\Console\Input\InputInterface;
	use Symfony\Component\Console\Output\OutputInterface;

	class CustomCliCommand extends Command
    {
    protected function configure()
    {
        $this->setName('custom-cli');
        $this->setDescription('Run custom CLI command');
        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // Your custom code here
        $output->write('Hello, world!');
    }
    }
	

Step 3: Register the CLI command

Add the following code to CustomCli/etc/command.xml:


		
	
    
        
            
            CustomCli
            CustomCli\Model\Command
        
    
	
	

Step 4: Enable the module

php bin/magento module:enable CustomCli


		php bin/magento module:enable CustomCli
	

Step 5: Run the CLI command

Run the following command in your terminal:


	php bin/magento custom-cli
	

LET’S WORK TOGETHER

We love working with Small Businesses, Retailers, Manufacturers. Our team is here to Help.

→ Schedule Free Consultant Now
Copyright © 2024 CommercePeer. All Rights Reserved.