@domenico
To use the Symfony console bundle to execute commands, you will need to follow these steps:
Here is an example of a command that outputs a message:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// src/Command/HelloCommand.php namespace AppCommand; use SymfonyComponentConsoleCommandCommand; use SymfonyComponentConsoleInputInputInterface; use SymfonyComponentConsoleOutputOutputInterface; class HelloCommand extends Command { protected static $defaultName = 'app:hello'; protected function configure() { $this ->setDescription('Outputs a hello message') ->setHelp('This command outputs a hello message to the console'); } protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Hello!'); } } |
Then, in the app/Console/Kernel.php
file, you can register the command:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// app/Console/Kernel.php namespace AppConsole; use AppCommandHelloCommand; use SymfonyComponentConsoleCommandCommand; use SymfonyComponentConsoleCommandLoaderCommandLoaderInterface; use SymfonyComponentConsoleCommandLoaderContainerCommandLoader; use SymfonyComponentConsoleInputInputInterface; use SymfonyComponentConsoleOutputOutputInterface; use SymfonyComponentConsoleSingleCommandApplication; use SymfonyComponentDependencyInjectionContainerInterface; use SymfonyComponentHttpKernelBundleBundleInterface; class Kernel { // ... protected function getCommandLoader(ContainerInterface $container): CommandLoaderInterface { $commands = [ new HelloCommand(), // ... ]; return new ContainerCommandLoader($container, $commands); } } |
Now, you can execute the app:hello
command by running the following command:
1
|
php bin/console app:hello |