php - 变量在 symfony2 中不存在

标签 php symfony twig

我的 symfony2 项目有以下问题:

这是我的 Controller 的代码

public function showCustomerAction($id) {
    // retrieve the customer from the database
    $em = $this->getDoctrine()->getManager();
    $customer = $em->getRepository('VictorIoSiteBundle:Customer')->find($id);

    //throw new \Exception($customer);
    return $this->render('VictorIoSiteBundle:Site:viewCustomer.html.twig', array('customer' => $customer));
}

还有我的 Twig View 的代码(非常简单):

{% if customer is defined %}
    <h3>Customer: {{ customer }} </h3>
{% endif %}

最后是我的 routing.yml

victor_io_site_show_customer:
pattern: /show-customer/{id}
defaults: { _controller: VictorIoSiteBundle:Site:showCustomer }
requirements:
    id: \d+

现在当我继续

http://localhost/Garage/web/app_dev.php/show-customer/46

我收到以下错误:

Variable " customer " does not exist in VictorIoSiteBundle:Site:viewCustomer.html.twig at line 2
500 Internal Server Error - Twig_Error_Runtime

最佳答案

看起来像是字符问题。有假空间char(194)在你的 {{ customer }} 中。
尝试只删除它们并添加一个真实的空间。

当您按下 Alt Gr + Space 时会出现此字符(我经常遇到)

提示

  • TWIG 会忽略空格,如果存在真正的空格问题也不会导致错误
  • 大多数时候,当你看到

    Variable "foo " does not exists

    这意味着你在 foo 变量之后有这个字符

关于php - 变量在 symfony2 中不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19495222/

相关文章:

php - 用于获取推文的服务器端或客户端?

php - Symfony/phpUnit : functional test of a form modifying an entity

php - 奇怪的问题 Doctrine/Symfony 请求

html - 手动渲染 Twig 字符串时禁用 HTML 转义

php - 格式化数据库中的日期编号

php - is_file 总是返回 false

php - 数据库中插入了乱码而不是希伯来语

javascript - Babel 6 + React - 传递初始数据(浏览器内转译器)

php - 未呈现模板

Twig /木材有条件显示 body 等级