Aşağıdaki örnek size genel anlamda CLI komutu yaratma konusunda bilgi veriyor ama siz istediğiniz gibi değiştirebilirsiniz. Yarattığınız komutu cron, build.xml, kendi class dosyalarınız vb. yerlerde çalıştırabilirsiniz.


Örnek


Aşağıdaki configure() methodunun birçok özelliği var, o nedenle dökümantasyonunu kontrol ederseniz iyi olur.


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");
}
}

Mevcut olan tüm komutların listesi


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

Kendi komutumuzun bilgilerini görme


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

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

Çalıştırma


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