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.

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

报告相同问题?

悬赏问题

  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗
  • ¥15 一个小问题,本人刚入门,哪位可以help
  • ¥30 python安卓开发
  • ¥15 使用R语言GD包一直不出结果