Symfony2 custom console command not working

I had this problem, and it was because the name of my PHP class and file didn't end with Command.

Symfony will automatically register commands which end with Command and are in the Command directory of a bundle. If you'd like to manually register your command, this cookbook entry may help: http://symfony.com/doc/current/cookbook/console/commands_as_services.html


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

If you override the default __construct method the Command will not be auto-registered by Symfony, so you have to either take the service approach as mentioned earlier or remove the __construct override and make that init step in the execute method or in the configure method.

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

It took me a moment to figure this out.


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

I just removed it and call the parent __construct() method (Symfony 2.7) and it worked well ;)


I figured out why it was not working: I simply forgot to register the Bundle in the AppKernel.php. However, the other proposed answers are relevant and might be helpful to resolve other situations!

By convention: the commands files need to reside in a bundle's command directory and have a name ending with Command.

in AppKernel.php

public function registerBundles()
{
    $bundles = [
        ...
        new MaintenanceBundle\MaintenanceBundle(),
    ];

    return $bundles;
}

Tags:

Symfony