3

I started to wtrite a command, following the doc and this si the command now(very very simple)

namespace App\Command;

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

// the name of the command is what users type after "php bin/console"
#[AsCommand(name: 'set-info')]
class CreateUserCommand extends Command
{
    protected static $defaultName = 'set-info';

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        // ... put here the code to create the user

        // this method must return an integer number with the "exit status code"
        // of the command. You can also use these constants to make code more readable

        // return this if there was no problem running the command
        // (it's equivalent to returning int(0))
        echo "Hello";
        return Command::SUCCESS;

        // or return this if some error happened during the execution
        // (it's equivalent to returning int(1))
        // return Command::FAILURE;

        // or return this to indicate incorrect command usage; e.g. invalid options
        // or missing arguments (it's equivalent to returning int(2))
        // return Command::INVALID
    }
}

but when I try to execute it php bin/console app:set-info I get [critical] Error thrown while running command "set-info". Message: "Command "set-info" is not defined."

Autoconfigure is true on services.yml

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

Do I missed something?

af_fgh
  • 55
  • 3

1 Answers1

1

You are calling the wrong command.

The protected static $defaultName = 'set-info'; is your command name.

  1. call:

php bin/console set-info

  1. Or change command name

protected static $defaultName = 'app:create-user';

psiess
  • 163
  • 1
  • 2
  • 12
  • It was an error copying the wrong command to write the question, the error persists – af_fgh May 29 '22 at 10:34
  • You have updated the command to php bin/console app:set-info but the command name is still protected static $defaultName = 'set-info'; – psiess May 29 '22 at 14:46