【问题标题】:Symfony 2.4 execute Command from ControllerSymfony 2.4 从控制器执行命令
【发布时间】:2014-03-20 13:55:56
【问题描述】:

我想从我的控制器执行命令 fos:elastica:populate。

我尝试了该代码,但它不起作用,我得到 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());

有什么想法吗?

我尝试不同的代码.....

    $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('无交互') 在填充命令中。

如果我将代码更改为:
$input = new StringInput('--no-interaction');

它说: '“--no-interaction”选项不存在。' 在
'ArgvInput ->addLongOption('无交互',null)'

【问题讨论】:

    标签: php symfony foselasticabundle


    【解决方案1】:

    一步一步,如何从控制器运行缓存清除命令:app/console cac:cle --env=(current_env)。

    首先,确保将命令注册为服务(service.yml):

    xxx.cache.clear:
        class: Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand
        calls:
            - [setContainer, ["@service_container"] ]
    

    Conde 在你的控制器中:

    $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 上测试

    【讨论】:

    • 使用 Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand 不工作(我只有一个错误,会话处于活动状态,但没关系。使用 FOS\ElasticaBundle\Command\PopulateCommand,它仍然说“不-交互”选项不存在。有没有办法给 FOS 选项?
    • @Sancho,所有你想传递的选项,你需要添加到 $input var 中。所以它会像```$input = new ArgvInput(array('--env=' . $this->container->getParameter('kernel.environment'), '-n'));
    【解决方案2】:

    首先:检查该命令是否已注册为服务。 如果不自己注册

    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
    

    如果已经注册,就按照上面的方式使用


    好的,我明白了:我的解决方案是另一种解决方案(如果捆绑的命令未在本机​​注册为服务,则可能更适合您自己的命令)。阅读一些文档,您的方法也应该有效。您遇到了一个阻止您的错误:

    $output = new ConsoleOutput; 应该是$output = new ConsoleOutput();

    【讨论】:

    • 现在我有:$output = new ConsoleOutput; $input = new ArrayInput(array("fos:elastica:populate")); $command = $this->get('FosElasticaPopulateService'); $命令->运行($输入,$输出);。它不起作用。我有 symfony 错误:““0”参数不存在”
    • 同样的错误:$output = new ConsoleOutput(); $command = $this->get('FosElasticaPopulateService'); $input = new ArrayInput(array("fos:elastica:populate")); $command->run($input, $output); .没有“简单”的方法来执行一个简单的命令?像 SymfonyExecute('fos:elastica:populate'); 这样的东西?
    • @Sancho:你在用get('FosElasticaPopulateService');做什么?我告诉过你,它们是两种替代解决方案。你的代码有错别字,请不要把所有的东西都弄乱了:)
    • 我什么都不懂...我使用 $output = new ConsoleOutput; $command = $this->get('FosElasticaPopulateService'); $command->run(null, $output);那是对的吗 ?它说'run() 必须是 InputInterface 的一个实例。
    • @Sancho:对不起,如果我很粗鲁,但是......你能读懂我写的东西吗?你打错字了!您必须在$output = new ConsoleOutput() 中修改$output = new ConsoleOutput 请在我的回答中的“行”下查看
    【解决方案3】:

    如果需要命令中的代码在控制器中执行,请将代码放在自己的类中,然后在控制器和命令中调用该类。

    【讨论】:

    • 我想执行一个捆绑提供的命令。我无法将代码从 fos:elastica 移动到另一个类。
    • 我明白了。您尝试执行的命令有一些必需的选项。 github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/… 可能你应该通过输入来设置它们。
    • “batch-size”选项是必需的,没有默认值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多