Symfony2 custom console command not working

I created a new class in src / MaintenanceBundle / Command, named it GreetCommand.php and entered the following code into it:

<?php namespace SK2\MaintenanceBundle\Command; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class GreetCommand extends ContainerAwareCommand { protected function configure() { $this ->setName('maintenance:greet') ->setDescription('Greet someone') ->addArgument('name', InputArgument::OPTIONAL, 'Who do you want to greet?') ->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters') ; } protected function execute(InputInterface $input, OutputInterface $output) { $name = $input->getArgument('name'); if ($name) { $text = 'Hello '.$name; } else { $text = 'Hello'; } if ($input->getOption('yell')) { $text = strtoupper($text); } $output->writeln($text); } } ?> 

And tried to call him through

application / console service: Fabien greeting

But I always get the following error:

[InvalidArgumentException] There are no commands in the service namespace.

Any ideas?

+6
source share
7 answers

I understood why it does not work: I just forgot to register the Bundle in AppKernel.php. However, the other suggested answers are relevant and may be helpful in resolving other situations!

By convention: the command files must be in the command directory of the package and have a name ending with the command.

in AppKernel.php

 public function registerBundles() { $bundles = [ ... new MaintenanceBundle\MaintenanceBundle(), ]; return $bundles; } 
+14
source

I had this problem, and this is because the name of my PHP class and file did not end with Command .

Symfony will automatically register commands that end in Command and are in the Command directory of the package. If you want to manually register your team, this cookbook entry may help: http://symfony.com/doc/current/cookbook/console/commands_as_services.html

+41
source

I had a similar problem and worked out another possible solution:

If you override the default __construct , the command will not be automatically registered by Symfony, so you will either have to use the service approach, as mentioned earlier, or remove the __construct override and do this initialization step in execution or in the configure method.

Does anyone really know a good best practice how to do init "stuff" in Symfony commands?

It took me a moment to figure this out.

+6
source

In addition to MonocroM's answer, I had the same problem with my team, and Symfony was silently ignored just because my command constructor had 1 required argument.

I just deleted it and called the parent method __construct() (Symfony 2.7) and it worked well;)

+1
source

In my case, it complained about the "workflow" namespace, although WorkflowDumpCommand was correctly provided by the framework.

However, it was not available to run because I did not define any worker processes, therefore the isEnabled() method of the returned command is false ,

+1
source

I think you need to call parent::configure() in your configure method

0
source

If you overload the command designer and use lazy-load / autowiring, your commands will not be automatically registered. To fix this, you can add the $defaultName variable:

 class SunshineCommand extends Command { protected static $defaultName = 'app:sunshine'; // ... } 

Link to Symfony Docs .

0
source

Source: https://habr.com/ru/post/897246/


All Articles