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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵