An example of a class of a certain command:

<?php namespace AppBundle\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class NewCommand extends Command { protected function configure() { $this ->setName('new-command') ->setDescription('Creates a new user.') ->setHelp('This command allows you to create a user...'); } protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('New command'); } } 

How does symfony find this class and register it as a console command? After all, when autoloading, it is impossible to determine which classes are inherited from the base class (in this case, the Command class).

    2 answers 2

    Symphony registers teams in two ways:

    • It registers all commands from all registered bundles that are in the Command directory (in some version symfony removed this feature, I don’t remember which one, in the 4th it probably is not)
    • Manual registration of commands through the container, that is, create a service and add a tag called console.command

      In order for it to work, you need to register the command in bin / console; see the documentation https://symfony.com/doc/3.4/components/console.html

       #!/usr/bin/env php <?php // application.php require __DIR__.'/vendor/autoload.php'; use Symfony\Component\Console\Application; $application = new Application(); // ... register commands $application->add(new \AppBundle\Command\NewCommand()); $application->run(); 

      That's how it finds him

      • Yes, but if you use this component regardless of the symfony framework. When used with a framework, it is enough to add a command class to make the command available in the console. - sh1da9440