Magento 2 - Пользовательская команда оболочки не работает


Я хочу выполнить некоторый код с помощью пользовательской команды , например:

php bin/magento mycommand:dothis

Чтобы включить эту команду, я добавил этот код в 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>

В моем файле .php

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

Код файла:

<?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;
    }
}

В оболочке я сталкиваюсь с этой ошибкой:

Команда "mycommand: dothis" не определена.

Author: Shoaib Munir, 2019-04-23

2 answers

У меня есть еще один способ. Вы можете использовать этот код:

Создать di.xml файл и добавьте этот код ниже:

<?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>

Затем вам нужно создать файл для команды create:

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.');
    }
}

Теперь выполните команду генерация: очистка . Он выполнит сгенерированную rm-r/* команду и очистит сгенерированную папку .

 2
Author: Rohan Hapani, 2019-04-23 13:23:27

Пожалуйста, измените конструкцию

Из Этого

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

До

public function __construct(
        State $appState
    ) {
        $this->appState = $appState;
        parent::__construct();
    }
 1
Author: , 2019-04-23 12:53:05