douyinliu8813 2018-08-13 08:18 采纳率: 100%
浏览 66
已采纳

为什么在symfony 3.4中呈现php模板是打印http标头?

I'm trying to move from a messy PHP template structure, with many duplicated code, to slots to help us having a similar twig philosophy with the extends.

The problem that we have is that some templates are rendering the HTTP Headers: enter image description here

The code for Form/form.html.php is:

<?php
/**
 * @var \Symfony\Bundle\FrameworkBundle\Templating\TimedPhpEngine    $view
 * @var \Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper $formHelper
 * @var \Symfony\Component\Templating\Helper\SlotsHelper             $slotsHelper
 * @var \Symfony\Component\Form\FormView                             $form
 */
$formHelper = $view['form'];
$slotsHelper = $view['slots'];

?>
<div class="form-wrapper">
    <?php $slotsHelper->output('form-start', $formHelper->start($form)) ?>
    <?php $slotsHelper->output('form-widget', $formHelper->widget($form)) ?>
    <?php $slotsHelper->output('form-end', $formHelper->end($form)) ?>
</div>

And the code for the other template, Form/Order/form.html.php, is

<?php
/**
 * @var \Symfony\Bundle\FrameworkBundle\Templating\TimedPhpEngine    $view
 * @var \Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper $formHelper
 * @var \Symfony\Component\Templating\Helper\SlotsHelper             $slotsHelper
 * @var \Symfony\Component\Form\FormView                             $form
 */
$formHelper = $view['form'];
$slotsHelper = $view['slots'];
$view->extend(':Form:form.html.php');

$slotsHelper->start('form-widget');
echo $formHelper->widget($form);
$slotsHelper->stop();

In the Controller, I'm rendering the form as always I've rendered a form:

$this->render("Form/Order/form.html.php", ['form' => $orderForm->createView()])

If I change this sentence by "returned string", Symfony prints only this "returned string", not the headers.

The form is rendered correctly, but for some reason that I cannot understand, Symfony is printing the HTTP Headers.

Thanks!

  • 写回答

1条回答 默认 最新

  • dongmangsha7354 2018-08-13 08:34
    关注

    Well, after asking I still researching about it and I finally found out why and thanks to this article.

    I've executed render, but render returns the rendered template as Response, therefore the HTTP Headers. renderView just return the template rendered.

    So I've just had to change the line:

    $this->render("Form/Order/form.html.php", ['form' => $orderForm->createView()])
    

    to

    $this->renderView("Form/Order/form.html.php", ['form' => $orderForm->createView()])
    

    Now, it works as expected.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效