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.

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站