【问题标题】:Using HTML.PHP instead of html.twig - not working使用 HTML.PHP 而不是 html.twig - 不工作
【发布时间】:2018-11-29 05:05:12
【问题描述】:
class DefaultController extends Controller
{
    public function indexAction($page, $name) {
        return $this->render('default/new.html.php'
        //         , array(
        //     $name => 'bob'
        // )
        );
    }
}

new.html.php

<p>Welcome to the index <?= $name; ?></p> 

当我使用此代码时,它会返回一条错误消息。

但是,

<p>Welcome to the index {{ name }}

这会返回正确的输出。

我想使用.html.php 而不是.html.twig

我正在经历这个https://symfony.com/doc/3.4/templating/PHP.html

routing.yml

app:
    path:      /dd
    defaults:
        _controller: AppBundle:Default:index
        page:        1
        name:       "bob"

config.yml

framework:
    # ...
    templating:
        engines: ['twig', 'php']

注意:我使用的是 ubuntu16.04 和 Symfony 3.4

【问题讨论】:

  • 什么是错误信息?
  • Controller "AppBundle\Controller\DefaultController::indexAction()" 要求您为 "$name" 参数提供一个值。参数可以为空且未提供空值、未提供默认值或因为在此参数之后有一个非可选参数。
  • 您将参数传递给操作,但您没有在路由中定义它。 symfony.com/doc/3.4/routing/extra_information.html
  • 现在我得到了输出,但“名称”仍未显示 - 我的输出是“欢迎来到索引”
  • @newbie stackoverflow.com/questions/21279901/… 可能不支持短 php 标签?

标签: php html symfony model-view-controller twig


【解决方案1】:

根据 Symfony 文档在本地尝试:

/**
 * @Route("/test", name="test_route")
 */
public function test()
{
    return $this->render(
        'test.html.php',
        [
            'test_var' => 'Hello test',
        ]
    );
}

我的test.html.php

<?php
echo $test_var;

这会输出Hello test

PS:试过get_defined_vars();,应该在那里看到变量。

【讨论】:

  • 试过...仍然是空白输出...这会是 ubuntu 中的 php 服务器的问题吗??
  • 可能是配置问题。 var_dump(get_defined_vars()); 输出什么?
  • 也许问题出在我的配置上...会检查一下..问题似乎在 php 中..因为 new.html.php不接受 php 内容> 仍然,该页面返回一个空白输出.. 如果我写了一些东西 out of php tag 那么它会显示在 new.html.php
  • 我也在 windows 中尝试过..但没有任何变化...我想更改 routing.yml 中的任何内容吗
  • 你在使用注解来路由吗?如果是这样 - 不。
猜你喜欢
  • 2014-04-08
  • 2021-10-23
  • 2020-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
相关资源
最近更新 更多