我想从我的控制器执行命令fos:elastica:populate。
我试过这个代码,但它不工作,我得到错误= 1的var_dump显示"
$command = 'fos:elastica:populate';
$app = new Application($this->get('kernel'));
$app->setAutoExit(false);
$input = new StringInput($command);
$output = new ConsoleOutput;
$error = $app->run($input, $output);
var_dump($error);
var_dump(stream_get_contents($output->getStream());
有什么想法吗?
我尝试不同的代码.....
$command = $this->get('FosElasticaPopulateService');
$input = new StringInput('');
$output = new ConsoleOutput();
ladybug_dump($input);
// Run the command
$retval = $command->run($input, $output);
if(!$retval)
{
echo "Command executed successfully!'n";
}
else
{
echo "Command was not successful.'n";
}
var_dump(stream_get_contents($output->getStream()));
它说:"不存在‘无交互’选项。"在Input ->getOption ('no-interaction')在populatcommand中。
如果我把代码改成:
$input = new StringInput('——no-interaction');
它说:"——no-interaction"选项不存在。"在
'ArgvInput ->addLongOption ('no-interaction', null)'
一步一步,如何运行缓存清除命令:app/console cac:cle——env=(current_env) from controller.
首先,确保将command注册为service (service.yml):
xxx.cache.clear:
class: Symfony'Bundle'FrameworkBundle'Command'CacheClearCommand
calls:
- [setContainer, ["@service_container"] ]
在你的控制器中编译:
$command = $this->container->get('xxx.cache.clear');
$input = new ArgvInput(array('--env=' . $this->container->getParameter('kernel.environment')));
$output = new ConsoleOutput();
$command->run($input, $output);
。
首先:检查该命令是否已注册为服务。如果没有自己注册
FosElasticaPopulateService:
class: Path'To'Bundle'Class
calls:
- [setContainer, ["@service_container"] ]
然后放入控制器
$output = new ConsoleOutput;
$command = $this->get('FosElasticaPopulateService');
$command->run(null, $ouput); //null here is for input parameters; if you need them, insert
如果它已经注册了,就像上面所示的那样使用
好吧,我明白了:我的解决方案是一个替代方案(如果bundle的命令没有本地注册为服务,可能更适合您自己的命令)。阅读一些文档,你的方法应该也能工作。你有一个错误阻止你:
$output = new ConsoleOutput;
应为$output = new ConsoleOutput();
如果您需要命令中的代码在控制器中执行,请将代码放入其自己的类中,并在控制器和命令中调用该类