Console Commands

The Symfony framework provides lots of commands through the bin/console script(e.g. the well-known bin/console cache:clear command). These commands arecreated with the Console component. You can alsouse it to create your own commands.

The Console: APP_ENV & APP_DEBUG

Console commands run in the environment defined in the APP_ENVvariable of the .env file, which is dev by default. It also reads the APP_DEBUGvalue to turn "debug" mode on or off (it defaults to 1, which is on).

To run the command in another environment or debug mode, edit the value of APP_ENVand APP_DEBUG.

Creating a Command

Commands are defined in classes extendingCommand. For example, you maywant a command to create a user:

  1. // src/Command/CreateUserCommand.php
  2. namespace App\Command;
  3.  
  4. use Symfony\Component\Console\Command\Command;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7.  
  8. class CreateUserCommand extends Command
  9. {
  10. // the name of the command (the part after "bin/console")
  11. protected static $defaultName = 'app:create-user';
  12.  
  13. protected function configure()
  14. {
  15. // ...
  16. }
  17.  
  18. protected function execute(InputInterface $input, OutputInterface $output)
  19. {
  20. // ...
  21. }
  22. }

Configuring the Command

You can optionally define a description, help message and theinput options and arguments:

  1. // ...
  2. protected function configure()
  3. {
  4. $this
  5. // the short description shown while running "php bin/console list"
  6. ->setDescription('Creates a new user.')
  7.  
  8. // the full command description shown when running the command with
  9. // the "--help" option
  10. ->setHelp('This command allows you to create a user...')
  11. ;
  12. }

The configure() method is called automatically at the end of the commandconstructor. If your command defines its own constructor, set the propertiesfirst and then call to the parent constructor, to make those propertiesavailable in the configure() method:

  1. // ...
  2. use Symfony\Component\Console\Command\Command;
  3. use Symfony\Component\Console\Input\InputArgument;
  4.  
  5. class CreateUserCommand extends Command
  6. {
  7. // ...
  8.  
  9. public function __construct(bool $requirePassword = false)
  10. {
  11. // best practices recommend to call the parent constructor first and
  12. // then set your own properties. That wouldn't work in this case
  13. // because configure() needs the properties set in this constructor
  14. $this->requirePassword = $requirePassword;
  15.  
  16. parent::__construct();
  17. }
  18.  
  19. protected function configure()
  20. {
  21. $this
  22. // ...
  23. ->addArgument('password', $this->requirePassword ? InputArgument::REQUIRED : InputArgument::OPTIONAL, 'User password')
  24. ;
  25. }
  26. }

Registering the Command

Symfony commands must be registered as services and taggedwith the console.command tag. If you're using thedefault services.yaml configuration,this is already done for you, thanks to autoconfiguration.

Executing the Command

After configuring and registering the command, you can execute it in the terminal:

  1. $ php bin/console app:create-user

As you might expect, this command will do nothing as you didn't write any logicyet. Add your own logic inside the execute() method.

Console Output

The execute() method has access to the output stream to write messages tothe console:

  1. // ...
  2. protected function execute(InputInterface $input, OutputInterface $output)
  3. {
  4. // outputs multiple lines to the console (adding "\n" at the end of each line)
  5. $output->writeln([
  6. 'User Creator',
  7. '============',
  8. '',
  9. ]);
  10.  
  11. // the value returned by someMethod() can be an iterator (https://secure.php.net/iterator)
  12. // that generates and returns the messages with the 'yield' PHP keyword
  13. $output->writeln($this->someMethod());
  14.  
  15. // outputs a message followed by a "\n"
  16. $output->writeln('Whoa!');
  17.  
  18. // outputs a message without adding a "\n" at the end of the line
  19. $output->write('You are about to ');
  20. $output->write('create a user.');
  21. }

Now, try executing the command:

  1. $ php bin/console app:create-user
  2. User Creator
  3. ============
  4.  
  5. Whoa!
  6. You are about to create a user.

Output Sections

The regular console output can be divided into multiple independent regionscalled "output sections". Create one or more of these sections when you need toclear and overwrite the output information.

Sections are created with thesection() method,which returns an instance ofConsoleSectionOutput:

  1. class MyCommand extends Command
  2. {
  3. protected function execute(InputInterface $input, OutputInterface $output)
  4. {
  5. $section1 = $output->section();
  6. $section2 = $output->section();
  7. $section1->writeln('Hello');
  8. $section2->writeln('World!');
  9. // Output displays "Hello\nWorld!\n"
  10.  
  11. // overwrite() replaces all the existing section contents with the given content
  12. $section1->overwrite('Goodbye');
  13. // Output now displays "Goodbye\nWorld!\n"
  14.  
  15. // clear() deletes all the section contents...
  16. $section2->clear();
  17. // Output now displays "Goodbye\n"
  18.  
  19. // ...but you can also delete a given number of lines
  20. // (this example deletes the last two lines of the section)
  21. $section1->clear(2);
  22. // Output is now completely empty!
  23. }
  24. }

Note

A new line is appended automatically when displaying information in a section.

Output sections let you manipulate the Console output in advanced ways, such asdisplaying multiple progress bars whichare updated independently and appending rows to tablesthat have already been rendered.

Console Input

Use input options or arguments to pass information to the command:

  1. use Symfony\Component\Console\Input\InputArgument;
  2.  
  3. // ...
  4. protected function configure()
  5. {
  6. $this
  7. // configure an argument
  8. ->addArgument('username', InputArgument::REQUIRED, 'The username of the user.')
  9. // ...
  10. ;
  11. }
  12.  
  13. // ...
  14. public function execute(InputInterface $input, OutputInterface $output)
  15. {
  16. $output->writeln([
  17. 'User Creator',
  18. '============',
  19. '',
  20. ]);
  21.  
  22. // retrieve the argument value using getArgument()
  23. $output->writeln('Username: '.$input->getArgument('username'));
  24. }

Now, you can pass the username to the command:

  1. $ php bin/console app:create-user Wouter
  2. User Creator
  3. ============
  4.  
  5. Username: Wouter

Read Console Input (Arguments & Options) for more information about console options andarguments.

Getting Services from the Service Container

To actually create a new user, the command has to access someservices. Since your command is already registeredas a service, you can use normal dependency injection. Imagine you have aApp\Service\UserManager service that you want to access:

  1. // ...
  2. use App\Service\UserManager;
  3. use Symfony\Component\Console\Command\Command;
  4.  
  5. class CreateUserCommand extends Command
  6. {
  7. private $userManager;
  8.  
  9. public function __construct(UserManager $userManager)
  10. {
  11. $this->userManager = $userManager;
  12.  
  13. parent::__construct();
  14. }
  15.  
  16. // ...
  17.  
  18. protected function execute(InputInterface $input, OutputInterface $output)
  19. {
  20. // ...
  21.  
  22. $this->userManager->create($input->getArgument('username'));
  23.  
  24. $output->writeln('User successfully generated!');
  25. }
  26. }

Command Lifecycle

Commands have three lifecycle methods that are invoked when running thecommand:

  • initialize() (optional)
  • This method is executed before the interact() and the execute()methods. Its main purpose is to initialize variables used in the rest ofthe command methods.
  • interact() (optional)
  • This method is executed after initialize() and before execute().Its purpose is to check if some of the options/arguments are missingand interactively ask the user for those values. This is the last placewhere you can ask for missing options/arguments. After this command,missing options/arguments will result in an error.
  • execute() (required)
  • This method is executed after interact() and initialize().It contains the logic you want the command to execute.

Testing Commands

Symfony provides several tools to help you test your commands. The mostuseful one is the CommandTesterclass. It uses special input and output classes to ease testing without a realconsole:

  1. // tests/Command/CreateUserCommandTest.php
  2. namespace App\Tests\Command;
  3.  
  4. use Symfony\Bundle\FrameworkBundle\Console\Application;
  5. use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
  6. use Symfony\Component\Console\Tester\CommandTester;
  7.  
  8. class CreateUserCommandTest extends KernelTestCase
  9. {
  10. public function testExecute()
  11. {
  12. $kernel = static::createKernel();
  13. $application = new Application($kernel);
  14.  
  15. $command = $application->find('app:create-user');
  16. $commandTester = new CommandTester($command);
  17. $commandTester->execute([
  18. 'command' => $command->getName(),
  19.  
  20. // pass arguments to the helper
  21. 'username' => 'Wouter',
  22.  
  23. // prefix the key with two dashes when passing options,
  24. // e.g: '--some-option' => 'option_value',
  25. ]);
  26.  
  27. // the output of the command in the console
  28. $output = $commandTester->getDisplay();
  29. $this->assertContains('Username: Wouter', $output);
  30.  
  31. // ...
  32. }
  33. }

Tip

You can also test a whole console application by usingApplicationTester.

Note

When using the Console component in a standalone project, useSymfony\Component\Console\Applicationand extend the normal \PHPUnit\Framework\TestCase.

Logging Command Errors

Whenever an exception is thrown while running commands, Symfony adds a logmessage for it including the entire failing command. In addition, Symfonyregisters an event subscriber to listen to theConsoleEvents::TERMINATE event and adds a logmessage whenever a command doesn't finish with the 0 exit status.

Learn More

The console component also contains a set of "helpers" - different smalltools capable of helping you with different tasks: