Creating a command
Creating your own custom commands is easy. Commands are defined in classes extending
Symfony\Component\Console\Command\Command
within your bundle. Below is a worked
example to create a basic command to ouput some text to the terminal.
namespace Custom\MyBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyCustomCommand extends Command
{
protected function configure()
{
$this->setName('my-custom-command')
->setDescription('A custom command');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->write('This is my custom command.');
}
}
The commands must be registered as services and tagged with the console.command
tag:
MyBundle\Command\MyCustomCommand:
tags:
- { name: console.command }
If you didn't do so already, you must ensure that your Bundle is registered with the Jadu application:
<bundles xmlns:config="http://www.jadu.co.uk/schema/config">
<core config:type="array">
<item key="my-bundle">Custom\MyBundle</item>
</core>
Make sure you clear the cache to update the site's configuration. You should now
see your command listed within php cli.php list
.
You can now run the command via cli.php
:
$ php cli.php my-custom-command
This is my custom command.
For more information you can refer to the Symfony Console Documentation