Creating a command

Commands are defined in classes extending Symfony\Component\Console\Command\Command within your bundle. For example, you may want a command to create a command to print 'This is my custom command'.

<?php

namespace 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 }

Make sure you clear the cache to update the sites configuration. You should now see your command listed within php cli.php list.

You can now run the command via cli.php:

shell script $ php cli.php my-custom-command This is my custom command.

For more information you can refer to the Symfony Console Documentation

results matching ""

    No results matching ""