3

I want to execute some code using custom command like:

php bin/magento mycommand:dothis

To enable this command I have added this code in di.xml

<type name="Magento\Framework\Console\CommandList">
    <arguments>
        <argument name="commands" xsi:type="array">
            <item name="do_this" xsi:type="object">Vendor\Module\Console\Command\DoThis</item>
        </argument>
    </arguments>
</type>

In my .php file

app/code/Vendor/Module/Console/Command/DoThis.php

File code is:

<?php

namespace Vendor\Module\Console\Command;

use Magento\Framework\App\State;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DoThis extends Command
{
    /**
     * @var State
     */
    protected $appState;

    public function __construct(
        State $appState
    ) {
        $this->appState = $appState;
        parent::__construct('mycommand');
    }

    /**
     * Configure cli command.
     */
    protected function configure()
    {
        $this->setName('mycommand')
            ->setDescription('This will run do this');
    }

    /**
     * Execute cli command
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return $this|int|null
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    protected function execute(
        InputInterface $input,
        OutputInterface $output
    ) {
        $this->appState->setAreaCode('adminhtml');

        //My code here

        $output->writeln('Process finished.');

        return $this;
    }
}

On shell, I am facing this error:

Command "mycommand:dothis" is not defined.

  • Let me know if any issue still you'll face. – Rohan Hapani Apr 23 at 13:23
2

I have one other way. You can use this code :

Create di.xml file and add this below code :

<?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="clean_generation" xsi:type="object">RH\Commands\Model\Generation</item>
            </argument>
        </arguments>
    </type>
</config>

Then, you need to create file for create command :

RH\Commands\Model\Generation.php

<?php
namespace RH\Commands\Model;

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

class Generation extends Command
{
    protected function configure()
    {
        $this->setName('generation:clean')->setDescription('Clean Generation Folder');
        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        system("rm -r generated/*");
        $output->writeln('Generation Folder Clean Successfully.');
    }
}

Now, execute generation:clean command. It will execute rm -r generated/* command & clear generated folder.

1

Please Change contructor

From This

public function __construct(
        State $appState
    ) {
        $this->appState = $appState;
        parent::__construct('mycommand');
    }

To

public function __construct(
        State $appState
    ) {
        $this->appState = $appState;
        parent::__construct();
    }
  • Above answer worked for me, Thanks anyways you gave time answering my question. +1 – Shoaib Munir Apr 24 at 4:26
  • I meant answer of Rohan Hapani worked for me, but still your answer is appreciated, so I gave you +1 – Shoaib Munir Apr 24 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.