Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

WARNING: You are browsing the documentation for Symfony 3.3 which is not maintained anymore. Consider upgrading your projects to Symfony 5.1.

How to Call Other Commands

3.3 version

How to Call Other Commands

If a command depends on another one being run before it, instead of asking the user to remember the order of execution, you can call it directly yourself. This is also useful if you want to create a “meta” command that just runs a bunch of other commands (for instance, all commands that need to be run when the project’s code has changed on the production servers: clearing the cache, generating Doctrine2 proxies, dumping Assetic assets, …).

Calling a command from another one is straightforward:

use Symfony\Component\Console\Input\ArrayInput;
// ...

protected function execute(InputInterface $input, OutputInterface $output)
    $command = $this->getApplication()->find('demo:greet');

    $arguments = array(
        'command' => 'demo:greet',
        'name'    => 'Fabien',
        '--yell'  => true,

    $greetInput = new ArrayInput($arguments);
    $returnCode = $command->run($greetInput, $output);

    // ...

First, you find() the command you want to execute by passing the command name. Then, you need to create a new Symfony\Component\Console\Input\ArrayInput with the arguments and options you want to pass to the command.

Eventually, calling the run() method actually executes the command and returns the returned code from the command (return value from command’s execute() method).


If you want to suppress the output of the executed command, pass a Symfony\Component\Console\Output\NullOutput as the second argument to $command->run().


Note that all the commands will run in the same process and some of Symfony’s built-in commands may not work well this way. For instance, the cache:clear and cache:warmup commands change some class definitions, so running something after them is likely to break.


Most of the times, calling a command from code that is not executed on the command line is not a good idea. The main reason is that the command’s output is optimized for the console and not to be passed to other commands.

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