Jaymin
Jaymin

Reputation: 1661

Creating Custom CLI Commands In Magento 2 thows me Error

I am new to Magento 2, I am creating a custom CLI Command which prints hello world but when I see the list php bin/magento list it doesnt show me my added commands instead it throws me this error :

 [Symfony\Component\Console\Exception\CommandNotFoundException]  
  There are no commands defined in the "training" namespace.  

Here are the things which I did to make CLI Command Work, I dont think so I am missing out something:

app/code/SimplifiedMagento/FirstModule/Console/Command/HelloWorld.php

<?php


namespace SimplifiedMagento\FirstModule\Console\Command;

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



class HelloWorld extends Command
{
    public function  configure()
    {
        $this->setName("training:hello_world");
        $this->setDescription("the command prints out hello world");
        parent::configure();

    }

    public function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('Hello World');
    }
}

app/code/SimplifiedMagento/FirstModule/etc/frontend/di.xml

<type name="Magento\Framework\Console\CommandList">
                <arguments>
                        <argument name="commands" xsi:type="array">
                                <item name="hello_world" xsi:type="object">
                                        SimplifiedMagento\FirstModule\Console\Command\HelloWorld</item>
                        </argument>
                </arguments>
        </type>

I am not sure where I am going wrong, can anyone help me out?

My Command would basically says training:hello_world

Upvotes: 0

Views: 900

Answers (1)

Jaymin
Jaymin

Reputation: 1661

After a bit research I found out that I have to create a seperate di.xml file inside my etc folder instead of etc/frontend/di.xml

I have cut the code of di.xml file of command and created a new file di.xml file inside etc folder and kept it and it worked.

<type name="Magento\Framework\Console\CommandList">
                <arguments>
                        <argument name="commands" xsi:type="array">
                                <item name="hello_world" xsi:type="object">
                                        SimplifiedMagento\FirstModule\Console\Command\HelloWorld</item>
                        </argument>
                </arguments>
        </type>

Upvotes: 0

Related Questions