What is causing a “Maximum function nesting level” error in Symfony 2.1 and Twig?

后端 未结 5 1947
滥情空心
滥情空心 2020-12-09 08:51

I have a project on Symfony 2.1. After updating composer components (Gedemo, Symfony core, Doctrine, Twig, etc..) I have the following error:

Fatal error: Ma         


        
相关标签:
5条回答
  • 2020-12-09 09:03

    Find the xdebug.ini file:

    $ locate xdebug.ini
    /etc/php5/conf.d/20-xdebug.ini
    /etc/php5/mods-available/xdebug.ini
    

    In my case the file is /etc/php5/conf.d/20-xdebug.ini. Open it and add this line:

    xdebug.max_nesting_level = 1000
    

    Don't forget to restart the FPM server.

    0 讨论(0)
  • 2020-12-09 09:05

    if increasing xdebug.max_nesting_level = 1000 failed check namespace is correct

    eg: namespace App\Entities; but folder name in lower case app

    then entity should contain namespace as namespace app\Entities;

    0 讨论(0)
  • 2020-12-09 09:07

    Just sharing a little tips to new Twig developers who might be interested to understand the "What is causing.." part of the question.

    Obvisouly in the original question the max nesting level is rather low (100) and as some comments mention, it might be too low for normal circumstances.

    However, if one increases the level to, say 256, 512 or even 1000 as adviced above and still hit the same error, then perhaps the most potential thing to look at would be the template inheritance. (The extends keywords on the first line of the templates)

    This is especially the case with projects where you have templates in multiple locations.

    Imagine an example project structure:

    ── plugins
       ├── your-plugin
       |   ├── views
       |   │   ├── base.twig
       │   │   ├── special-element.twig
       │   │   ├── some-other-element.twig
    ── theme
       ├── base.twig
       ├── index.twig
       ├── sub-page.twig
    

    The plugin has a template base.twig which extends the base.twig under the theme. But if the template locations are not correctly configured, the templates might end up extending itself again and again causing the infinite loop.

    How to check if this is the case? I'd be happy to hear about more accurate solutions, but one can start - for debugging purposes only - as simple as referring to the parent templates with the full server paths:

    {% extends "/var/www/path-to-your-template/" %}

    If it starts to work with absolute paths, then you can be quite sure there is something wrong with the template paths. Read more about it here: Twig Template Naming and Locations

    0 讨论(0)
  • 2020-12-09 09:10

    it is an error code that causes an infinite loop, but it happens from time to time that treatment without error exceeds the 100 calls nested functions.

    To correct this, open the php.ini, xdebug section and add the following line (putting what you want instead of 150)

     [xdebug]
    xdebug.max_nesting_level = 150
    
    0 讨论(0)
  • 2020-12-09 09:23

    In my case, I had to increase the amount of memory used by PHP in php.ini to 512MB. Also, I made a composer update on the site root folder to update the default settings generated by Symfony.

    0 讨论(0)
提交回复
热议问题