Console commands

Make a command

A console command is a PHP class that performs one task such as sending reports or cleaning old records. The generator creates the class file with the command name and a ready execute method.

Name custom commands with the app prefix so they never clash with built in Symfony commands. Keep each command focused on a single job that is easy to run by hand or by cron.

php bin/console make:command

created src/Command/HelloCommand.php

Run it

Run commands from the project root with the PHP binary followed by the console script and the command name. Symfony boots the kernel, runs the command, and prints the result to the terminal.

Check the exit code after the run because zero means success and any other value means failure. Use the same call in scheduled jobs so production behavior matches local testing.

php bin/console app:hello

Hello from Symfony

Arguments

Arguments are required positional values such as a user name or a record id. Declare them in the configure method and read them from the input object inside execute.

Mark an argument as required when the command cannot run without it. Validate the value early and print a clear message when it is missing or malformed.

namespace App\Command; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; #[AsCommand(name: "app:hello")] class HelloCommand extends Command { protected function configure(): void { $this->addArgument("name", InputArgument::REQUIRED, "Who to greet"); } protected function execute(InputInterface $input, OutputInterface $output): int { $name = $input->getArgument("name"); $output->writeln("Hello " . $name); return Command::SUCCESS; } }

Options

Options are named flags that change command behavior, for example a shout flag that uppercases output. They are optional by design and always have a default value.

Read an option with getOption and branch on its value inside execute. Prefer short and long names for frequently used flags so typing stays fast.

namespace App\Command; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; #[AsCommand(name: "app:hello")] class HelloCommand extends Command { protected function configure(): void { $this->addArgument("name", InputArgument::REQUIRED, "Who to greet"); $this->addOption("shout", "s", InputOption::VALUE_NONE, "Uppercase the greeting"); } protected function execute(InputInterface $input, OutputInterface $output): int { $name = $input->getArgument("name"); if ($input->getOption("shout")) { $name = strtoupper($name); } $output->writeln("Hello " . $name); return Command::SUCCESS; } }

Next: Events

Article author: Arthur Isaev