如何从控制器执行控制台命令

时间:2013-12-15 18:16:02

标签: symfony console

我正在使用symfony 2.3

我在ACME\TopBundle\Command\CrawlerCommand.php

中有命令

我在控制台中使用此命令。

$ app/console top:crawler 

但现在我想从Controller执行命令。

public function indexAction(){

    // I want to execute command 
}

我该怎么做?

我正在尝试@Amine建议的解决方案。

我有两个问题。

1)如何检查控制台输出?

我检查了console output class方法。

但是

$output->getStream()

它不会显示控制台日志。

1 个答案:

答案 0 :(得分:3)

最好的方法是将您的命令声明为服务

MyCommandService:
class: MyBundle\Command\MyCommand
calls:
    - [setContainer, ["@service_container"] ]

并在您的控制器中调用它

    use Symfony\Component\Console\Input\ArgvInput;
    use Symfony\Component\Console\Output\ConsoleOutput;
    .
    .
    public function myAction() {
      $command = $this->get('MyCommandService');

      $input = new ArgvInput(array('arg1'=> 'value'));
      $output = new ConsoleOutput();
      $command->run($input, $output);
   }

或者你可以使用这个例子:https://gist.github.com/predakanga/3487705

我更喜欢第一种解决方案。

相关问题