How to Define Commands as Services

How to Define Commands as Services

If you’re using the default services.yaml configuration, your command classes are already registered as services. Great! This is the recommended setup.

Note

You can also manually register your command as a service by configuring the service and tagging it with console.command.

For example, suppose you want to log something from within your command:

  1. namespace App\Command;
  2. use Psr\Log\LoggerInterface;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Output\OutputInterface;
  6. class SunshineCommand extends Command
  7. {
  8. protected static $defaultName = 'app:sunshine';
  9. private $logger;
  10. public function __construct(LoggerInterface $logger)
  11. {
  12. $this->logger = $logger;
  13. // you *must* call the parent constructor
  14. parent::__construct();
  15. }
  16. protected function configure(): void
  17. {
  18. $this
  19. ->setDescription('Good morning!');
  20. }
  21. protected function execute(InputInterface $input, OutputInterface $output): int
  22. {
  23. $this->logger->info('Waking up the sun');
  24. // ...
  25. return 0;
  26. }
  27. }

If you’re using the default services.yaml configuration, the command class will automatically be registered as a service and passed the $logger argument (thanks to autowiring). In other words, you only need to create this class and everything works automatically! You can call the app:sunshine command and start logging.

Caution

You do have access to services in configure(). However, if your command is not lazy, try to avoid doing any work (e.g. making database queries), as that code will be run, even if you’re using the console to execute a different command.

Note

In previous Symfony versions, you could make the command class extend from Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand to get services via $this->getContainer()->get('SERVICE_ID'). This is deprecated in Symfony 4.2 and it won’t work in future Symfony versions.

Lazy Loading

To make your command lazily loaded, either define its $defaultName static property:

  1. class SunshineCommand extends Command
  2. {
  3. protected static $defaultName = 'app:sunshine';
  4. // ...
  5. }

Or set the command attribute on the console.command tag in your service definition:

  • YAML

    1. # config/services.yaml
    2. services:
    3. # ...
    4. App\Command\SunshineCommand:
    5. tags:
    6. - { name: 'console.command', command: 'app:sunshine' }
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd">
    7. <services>
    8. <!-- ... -->
    9. <service id="App\Command\SunshineCommand">
    10. <tag name="console.command" command="app:sunshine"/>
    11. </service>
    12. </services>
    13. </container>
  • PHP

    1. // config/services.php
    2. use App\Command\SunshineCommand;
    3. // ...
    4. $container->register(SunshineCommand::class)
    5. ->addTag('console.command', ['command' => 'app:sunshine'])
    6. ;

Note

If the command defines aliases (using the [getAliases()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Command/Command.php "Symfony\Component\Console\Command\Command::getAliases()") method) you must add one console.command tag per alias.

That’s it. One way or another, the SunshineCommand will be instantiated only when the app:sunshine command is actually called.

Note

You don’t need to call setName() for configuring the command when it is lazy.

Caution

Calling the list command will instantiate all commands, including lazy commands.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.