Symfony 2.4从控制器执行命令 [英] Symfony 2.4 execute Command from Controller
问题描述
我想从控制器执行命令fos:elastica:populate.
I want to execute the command fos:elastica:populate from my controller.
我尝试了该代码,但是它不起作用,我得到了错误= 1 var_dump显示"
I tried that code but it doesn't work, i get error = 1 the var_dump show ""
$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());
有什么想法吗?
我尝试使用其他代码.....
I try a different code.....
$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()));
它说: 不互动"选项不存在." 在 输入-> getOption(无交互") 在PopulateCommand中.
It say: 'The "no-interaction" option does not exist.' at Input ->getOption ('no-interaction') in the PopulateCommand.
如果我使用以下代码更改代码:
$ input = new StringInput('-no-interaction');
if i change my code with:
$input = new StringInput('--no-interaction');
它说:
"--no-interaction"选项不存在.
在
'ArgvInput-> addLongOption('no-interaction',null)'
It say:
'The "--no-interaction" option does not exist.'
at
'ArgvInput ->addLongOption ('no-interaction', null) '
推荐答案
分步介绍如何运行缓存清除命令:app/console cac:cle --env =(current_env)from controller.
Step by step, how to run cache clear command: app/console cac:cle --env=(current_env) from controller.
首先,请确保将命令注册为服务(service.yml):
At first, make sure to register command as 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);
仅此而已. |在symfony 2.4上进行了测试
That's all. | tested on symfony 2.4
这篇关于Symfony 2.4从控制器执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!