500 error on knp_menu_render / "Memory exhausted" in TWIG/Environment.php

615 views Asked by At

I'm trying to use KnpMenuBundle with Symfony2 & TWIG.

In my twig template the menu is called like this:

<div id="before"></div>
{% set myMenu = knp_menu_get('MyBundle:Builder:myMenu') %} 
{#{{dump(seeMenu)}}#}
{{ knp_menu_render(seeMenu) }}
<div id="after"></div>

When I try to display the corresponding page, it returns a 500 error with no further details. The rendering stops at the {{ knp_menu_render(seeMenu) }} line, and the error disappears when I remove or comment this line.

Apache crashes due to this error, I have to reboot it after each failed test of rendering.

My Builder class seems OK, because the dump(seeMenu) shows me what I expect.

I didn't add anything in my config.yml about knp (as the doc says it is optionnal). I tried anyway, same problem.

I tried to render the menu via MopaBootstrapBundle ({{ mopa_bootstrap_menu(seeMenu) }}), same problem.

Firebug doesn't give more info about the 500 error and the dev.log file isn't more helpfull.


[EDIT] Apache log says:

PHP Fatal error:  Allowed memory size of 268435456 bytes exhausted (tried to allocate 130968 bytes) in /var/www/myproject/app/cache/dev/classes.php on line 3148

This explains Apache's crash. I tried to increase the memory (ini_set('memory_limit', '256M'); in app_dev.php) but the problem remains, which means that there is an infinite loop somewhere...


[EDIT2] After some research, it seems that the problem comes from the line 348 of the file Environment.php: dump(new $cls($this)); generates a "memory exhausted" error.

I have no idea why and where to look further. I use the same version of TWIG (1.20) with the same version of knpMenuBundle (2.0) in another project and it works fine...


[EDIT3] I'm still unable to make this KnpMenuBundle work :/ I tried to re-install the whole project but it doesn't change anything. I'm completely stuck and I have no clue what to do now..

1

There are 1 answers

0
Álvaro García On

TL;DR: Change your custom menu template name of knp_menu.html.twig

I know that this is a very old question but since I got this error today, maybe more people would suffer this trouble in the future.

In my case the problem was just an infinite recursion of the menu twig template.

If you have a custom template called knp_menu.html.twig with this line as the first line:

{% extends 'knp_menu.html.twig' %} you are getting an infinite recursion because twig are extending the same template instead of the "original" knp_menu template.

Just change your custom template name.

Maybe you updated the symfony packages because this trouble is only in newer versions.