我的 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/