use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class GreetUserCommand extends Command { protected function configure() { $this->setName('greet:user') ->setDescription('Greet the user'); } protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Hello, user!'); } } // Instantiate the command and run it $command = new GreetUserCommand(); $command->run($input, $output); // Output: Hello, user! echo $command->getName(); // greet:user
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class CreatePostCommand extends Command { protected static $defaultName = 'post:create'; protected function configure() { $this->setDescription('Create a new blog post'); } protected function execute(InputInterface $input, OutputInterface $output) { // Code to create a new blog post } } // Instantiate the command and run it $command = new CreatePostCommand(); $command->run($input, $output); // Output: Nothing (as there is no output in this command) echo $command->getName(); // post:createIn this example, we create a CLI command called "post:create", which creates a new blog post when executed. The getName() method returns its name as "post:create". The Symfony\Component\Console\Command\Command class is part of the Symfony Console component/library, which is used to create CLI commands in PHP applications.