Usage

Use src/Command/DemoCommand.php as an example when creating a new command. Set the name and description in the #[AsCommand] attribute: Symfony Console reads them from there, so a command does not need a configure() method to set them. Keep the name in a DEFAULT_NAME class constant, and use the constant in the attribute and in config/autoload/cli.global.php. Then the command name is written in one place only.

DemoCommand also implements configure() to show where a command can be extended by chaining methods, for example setHelp(), addArgument() and addOption(). For the demo, its configure() sets the same name and description as the attribute. Values set in configure() override the attribute, so if you keep both in your own command, keep them identical.

<?php

declare(strict_types=1);

namespace App\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
    name: ExampleCommand::DEFAULT_NAME,
    description: 'Example command description.',
)]
class ExampleCommand extends Command
{
    public const string DEFAULT_NAME = 'app:example';

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $output->writeln('Hello from app:example');

        return Command::SUCCESS;
    }
}

Then register it in config/autoload/cli.global.php:

'commands' => [
    App\Command\ExampleCommand::DEFAULT_NAME => App\Command\ExampleCommand::class,
],

Running

Using the command line, go to your application's root directory, then type the following command:

php ./bin/cli.php demo:command
On this page