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:
<?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!');
}
}
<?php
use Symfony\Component\Console\Application;
require __DIR__.'/vendor/autoload.php';
$application = new Application();
$application->add(new CustomCommand());
$application->run();
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.