代码之家  ›  专栏  ›  技术社区  ›  FortuneSoldier

Symfony命令无法工作,因为它会呈现模板

  •  0
  • FortuneSoldier  · 技术社区  · 7 年前

    我已经使用Symfonies生成了命令

    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应用程序/控制台触发器构建Symfony给了我这个错误行。 enter image description here

    有人能帮我一下,让这个命令对我有用吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Mcsky    7 年前

    您正在操纵 HTTP Response 在里面 pageAction 方法,我认为您使用了来自twig的错误方法。

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

    正如Cerad在评论中指出的 你只需要打电话 $content = $this->getContainer()->get('templating')->render($template, []);

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

    PS:从另一个“上下文”复制/粘贴时,请重新读取代码:D