【问题标题】:Symfony command doesn't work because it renders a templateSymfony 命令不起作用,因为它呈现模板
【发布时间】:2018-02-07 21:48:41
【问题描述】:

我已经使用 Symfonys 生成了命令

php app/console generate:command

它生成了这个命令并添加了

class AppTriggerBuildCommand extends ContainerAwareCommand
{
protected function configure()
    {
        $this
            ->setName('trigger')
            ->setDescription('...')
            ->addArgument('argument', InputArgument::OPTIONAL, 'Argument description')
            ->addOption('option', null, InputOption::VALUE_NONE, 'Option description')
        ;
    }

protected function execute(InputInterface $input, OutputInterface $output)
{
    $argument = $input->getArgument('argument');
    if($argument == 'build'){
        $content = $this->pageAction('en', 'home');
        $this->storeContentElementsAction('en', 'home', $content);
    }

    $output->writeln('You are outside build.');
}


public function pageAction($language, $page) {
    $akeneo = $this->getContainer()
        ->get("pimc.akeneo_cms.backend_connector");

    $contentElements = $akeneo->getContentElementList($page);
    $contentElements = $this->sort($contentElements);
    $content = $this->getContainer()->get('templating')->renderResponse(
        'AppBundle::page.html.twig',
        ["contentElements" => $contentElements, "language" => $language]);

    return $content;

}

private function sort($contentElements) {
    ksort($contentElements);
    return $contentElements;
}

/**
 * @param $language
 * @param $page
 */
public function storeContentElementsAction($language, $page, \Symfony\Component\HttpFoundation\Response $content) {

    $staticalContent = new StaticContent();
    $staticalContent->setData($content->getContent());
    $staticalContent->setBuild(1);
    $staticalContent->setName("/".$language."/".$page.".html");

    $doctrine = $this->getContainer()->get('doctrine');
    $em = $doctrine->getManager();
    $em->persist($staticalContent);
    $em->flush();
}

}

StaticContent 是一个实体。

但是当我在命令行中调用这个命令时,我遇到了一个我无法解决的异常错误。 php app/console trigger build Symfony 给了我这个错误行。

有人可以帮助我让这个命令对我有用吗?

【问题讨论】:

  • 将 renderResponse 改为 render。这将使您摆脱这个特定的错误。可能还有更多。
  • 嗯,好的。我已将它从 renderResponse 更改为 render,但我仍然有同样的错误。我做错了什么?

标签: php symfony doctrine twig symfony-2.8


【解决方案1】:

您在pageAction 方法中操作HTTP Response,我认为您使用了twig 中的错误方法。

您似乎希望将返回的 html 作为 html 字符串存储到数据库中。

正如 Cerad 在 cmets 中指出的那样,您只需致电 $content = $this->getContainer()->get('templating')->render($template, []);

现在 $content 是您呈现的 html 字符串。

PS:当您从另一个“上下文”复制/粘贴时重新阅读您的代码:D

【讨论】:

  • 是的,我想将 html 字符串存储到我的数据库中。我尝试将它从 renderResponse 更改为 render,但仍然出现相同的错误?!
  • 您能否尝试将get('templating') 替换为get('twig') 并使用-vvv 输出运行您的命令。将堆栈跟踪链接到您的问题,以确定您的代码在哪个供应商类别中
  • 不知道 -vvv。谢谢你的麦克斯基。所以我已经弄清楚这个错误是怎么回事。但它提出了不同的错误。它需要一个 Response 实例,我只给了它字符串 $content。这是确切的错误:类型错误:传递给 AppBundle\Command\AppTriggerBuildCommand::storeContentElementsAction() 的参数 3 必须是 Symfony\Component\HttpFoundation\Response 的实例,给定字符串,在 /var/www/html/iclei/ 中调用src/AppBundle/Command/AppTriggerBuildCommand.php 第 30 行
  • storeContentElementsAction's原型中将\Symfony\Component\HttpFoundation\Response $content替换为$htmlStringContent就可以了:)
  • 这是个笑话。对 ?我不太明白。我需要 Response 类的适当回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
  • 2020-09-04
  • 2017-02-01
  • 2020-10-17
相关资源
最近更新 更多