I
I
Ilya Loopashko2020-09-05 18:39:11
symfony
Ilya Loopashko, 2020-09-05 18:39:11

How to fix an error when creating a team?

I create my own command, when testing the command, an error occurs:

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2020-09-05
@deadloop

This means that you did not pass an argument to the command through the container. The container
itself can automatically substitute arguments for the command if these are other services and at the same time FQCN, in the case of primitives, either explicitly pass the values ​​or you need to bind

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question