Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable does not exist in symfony2

Tags:

php

twig

symfony

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
like image 873
Victor Avatar asked Oct 21 '13 12:10

Victor


1 Answers

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

like image 157
Touki Avatar answered Nov 05 '22 22:11

Touki