Reference / Symfony
/

Console Commands

CLI scripts that run inside the Symfony container — for cron jobs, maintenance, data backfills, scaffolding.

Foundational
  • Extend `Command` and use `#[AsCommand(name: 'app:do-thing')]`. Run via `bin/console app:do-thing`.
  • Implement `execute(InputInterface $input, OutputInterface $output): int` — return 0 for success.
  • Define args and options in `configure()` (or via `#[Argument]` / `#[Option]` attributes in modern Symfony).
  • Use `SymfonyStyle` for nicely formatted output: `$io->success()`, `$io->table()`, `$io->progressBar()`.
  • Inject services via the constructor, exactly like a controller.
#[AsCommand(name: 'app:reindex', description: 'Rebuild the search index')]
final class ReindexCommand extends Command
{
    public function __construct(private Indexer $indexer) { parent::__construct(); }

    protected function execute(InputInterface $in, OutputInterface $out): int
    {
        $this->indexer->rebuild();
        return Command::SUCCESS;
    }
}

Common gotchas

  • Don't print to stdout via `echo` — use the `OutputInterface`. It respects verbosity flags.
  • Commands invoked from the web (rare) lose the request context; design them to run standalone.