如何从控制器执行控制台命令 [英] How to execute console command from the controller
问题描述
我使用symfony 2.3
I am using symfony 2.3
我在 ACME\TopBundle\Command\CrawlerCommand.php
我使用这个命令从控制台。
I use this command from console.
$ app/console top:crawler
但现在我想从控制器执行命令。
But now I want to execute command from the Controller.
public function indexAction(){
// I want to execute command
}
我如何做?
我正在尝试@Amine建议的解决方案。
I am trying the solution that @Amine suggested.
我有两个问题。
我可以检查控制台输出吗?
1) How can I check the console output?
我已经检查了 console output class method。
I have checked the console output class method.
但
$output->getStream()
控制台日志。
推荐答案
最好的方法是将您的命令声明为服务
the best way is to declare your command as service
MyCommandService:
class: MyBundle\Command\MyCommand
calls:
- [setContainer, ["@service_container"] ]
并在您的控制器中调用如下
and in your controller call it like this
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
or you can use this exemple: https://gist.github.com/predakanga/3487705
我更喜欢第一个解决方案。
i prefer first solution.
这篇关于如何从控制器执行控制台命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!