deadloop
@deadloop
Активно осваиваю PHP

Как исправить ошибку при создании команды?

Создаю свою команду, при тестирование команды выпадает ошибка:

версия Symfony 4.4

php bin/console app:setup-wallpapers

In DefinitionErrorExceptionPass.php line 54:

Cannot autowire service "App\Command\SetupWallpapersCommand": argument "$rootDir" of method "__construct()" is type
-hinted "string", you should configure its value explicitly.

<?php

namespace App\Command;

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

class SetupWallpapersCommand extends Command
{
    /**
     * @var string
     */
    private $rootDir;

    public function __construct(string $rootDir)
    {
        parent::__construct();
        $this->rootDir = $rootDir;
    }
    protected static $defaultName = 'app:setup-wallpapers';
    protected function configure()
    {
        $this
            ->setDescription('Add a short description for your command')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $wallpapers = glob($this->rootDir . '/public/images/*.*');
        exit(\Doctrine\Common\Util\Debug::dump($wallpapers));
        $output->writeln('Command result.');
    }
}
  • Вопрос задан
  • 225 просмотров
Решения вопроса 2
Maksclub
@Maksclub
maksfedorov.ru
Это значит, что через контейнер вы не передали в команду аргумент

В команду сам контейнер может автоматом подставить аргументы, если это другие сервисы и одновременно FQCN, в случае примитивов — значения или явно передайте или нужно забиндить
Ответ написан
Комментировать
@bkosun
The configure() method is called automatically at the end of the command constructor. If your command defines its own constructor, set the properties first and then call to the parent constructor, to make those properties available in the configure() method:

//...
    public function __construct(bool $requirePassword = false)
    {
        // best practices recommend to call the parent constructor first and
        // then set your own properties. That wouldn't work in this case
        // because configure() needs the properties set in this constructor
        $this->requirePassword = $requirePassword;

        parent::__construct();
    }
//...



https://symfony.com/doc/4.4/console.html#configuri...
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы