Back to symfony.com

Lesson 10 of 11 · 3 min

Click any paragraph or press to highlight its code

The console

Every Symfony app includes a console. Writing your own commands takes one class: the #[AsCommand] attribute registers it and gives it a name.

Arguments and options are declared as method parameters with attributes, and SymfonyStyle gives you beautiful output, tables, progress bars and interactive questions for free.

The console also comes packed with built-in commands: inspect routes and services, clear caches, run migrations... and the make:* commands, which generate controllers, entities, forms, tests and more, so you never start from a blank file.

<?phpnamespace App\Command;use Symfony\Component\Console\Attribute\Argument;use Symfony\Component\Console\Attribute\AsCommand;use Symfony\Component\Console\Style\SymfonyStyle;#[AsCommand(    name: 'app:send-newsletter',    description: 'Sends the weekly newsletter',)]class SendNewsletterCommand{    public function __invoke(        SymfonyStyle $io,        #[Argument] string $segment = 'all',    ): int {        $io->title("Sending the newsletter to the '$segment' segment");        // ... your business logic        $io->success('Newsletter sent!');        return 0;    }}
$ php bin/console app:send-newsletter premium$ php bin/console make:controller$ php bin/console debug:router