Example below gives you the basics of creating a CLI example so you can extend it as you wish. You can then run this command in cron, build.xml, your classes so on.


Example


Method configure() has many other options so you can check the documentation.


namespace Application\BackendBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class HelloCommand extends ContainerAwareCommand
{
private $secret;
private $container;

/**
* Definition of command
*/
protected function configure()
{
$this
->setName('say:hello')
->addArgument(
'name',
InputArgument::REQUIRED,
'What is the name?'
)
->addArgument(
'surname',
InputArgument::OPTIONAL,
'What is the surname?'
)
->setDescription('This says hello to the person');
}

/**
* This is like a __construct()
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->secret = $this->getContainer()->getParameter('secret');
$this->container = $this->getContainer();
//$this->myService = $this->getContainer()->get('my_service');
}

/**
* Where actual process takes place
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$surname = $input->getArgument('surname');

$output->writeln("Hello $name $surname");
}
}

List all available console commands


php app/console list --no-debug
...
...
say
say:hello This says hello to the person
...
...

Getting details of our command


php app/console say:hello --help
Usage:
say:hello name [surname]

Arguments:
name What is the name?
surname What is the surname?

Run


php app/console say:hello inanzzz
Hello inanzzz
php app/console say:hello inanzzz surname
Hello inanzzz surname