meta charset='UTF-8'/> How to Add Command line in to Console CLI in Magento 2 | Magento 2 Store
Rated 5/5 based on 100 votes

How to Add Command line in to Console CLI in Magento 2

magento 2 add command line

In this article, we will find how to add a command line into magento 2 console CLIMagento 2 add command line use a interface to quick change some features like enable/disable cache, setup sample data… Before we start, please take some minutes to know about the naming in Magento 2 CLI.
We will use an example module Mageplaza_Example to demo for this lesson. To add an option to Magento 2 CLI, we will follow by some steps:

Step 1: Define command in di.xml

In di.xml file, you can use a type with name Magento\Framework\Console\CommandList to define the command option.
File: app/code/Mageplaza/HelloWorld/etc/di.xml ~~~
Mageplaza\HelloWorld\Console\Sayhello

This config will declare a command class `Sayhello`. This class will define the command name and `execute()` method for this command.

## Step 2: Create command class

As define in di.xml, we will create a command class:

File: `app/code/Mageplaza/HelloWorld/Console/Sayhello.php`

<?php namespace Mageplaza\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(‘example:sayhello’); $this->setDescription(‘Demo command line’); } protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln(“Hello World”); } } ~~~
In this function, we will define 2 methods:
  • configure() method is used to set the name and the description of the magento 2 add command line
  • execute() method will run when we call this command line via console.
After declare this class, please flush Magento cache and type this command:
php magento --list
You will see the list of all commands. Our command will be show here
magento 2 add command line
Now you can run the command to see the result
magento 2 add command line

Share on Google Plus

About Hoàng Kang

Name: Nguyễn Tuấn Hoàng

 photo 1f451_zpsp5spiubu.png If you're good at something, never do it for free  photo 1f451_zpsp5spiubu.png

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment