简体   繁体   中英

Variable does not exist in symfony2

I have the following problem on my symfony2 project:

This is the code of my 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));
}

And the code of my twig view (quite simple):

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

Finally my routing.yml

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

Now when I go on

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

I get the following error :

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

It looks like a characters issue. There are fake spaces char(194) in your {{ customer }} .
Try just removing them and add a real space instead.

This character occurs when you hit Alt Gr + Space (it happens to me alot)

Hints

  • TWIG ignores spaces and wouldn't result in an error if there was a real space issue
  • Mostly, when you see

    Variable "foo " does not exists

    it means you have this character after the foo variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM