How to use Symfony’s Console component for custom commands?

Symfony’s Console component allows you to create custom console commands easily by extending the Symfony\Component\Console\Command\Command class. Here's how you can use Symfony’s Console component for custom commands:

  1. Create a new PHP class that extends Symfony\Component\Console\Command\Command:
<?php use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class CustomCommand extends Command { protected static $defaultName = 'custom:command'; protected function configure() { $this->setDescription('This is a custom command'); } protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Hello from your custom command!'); } }
  1. Register the command in your application’s console application:
<?php use Symfony\Component\Console\Application; require __DIR__.'/vendor/autoload.php'; $application = new Application(); $application->add(new CustomCommand()); $application->run();
  1. Run the command in your terminal:
php your_console_application.php custom:command

Your custom command should now run and output 'Hello from your custom command!' in the terminal. You can add more functionality to your command by processing input arguments and options within the execute method.