I am getting the following error while try to use dump function in the template:
[2/2] Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("Notice: A non well formed numeric value encountered") in genus\show.html.twig at line 1
[1/2] ContextErrorException: Notice: A non well formed numeric value encountered
LOGS:
CRITICAL - Uncaught PHP Exception Twig_Error_Runtime: "An exception has been thrown during the rendering of a template ("Notice: A non well formed numeric value encountered") in "genus/show.html.twig" at line 1." at C:\Data\Literature\Programming\Source Code\Symfony\Symfony 3\aqua_note\vendor\twig\twig\lib\Twig\Template.php line 396
I actually got an error of type ContextErrorException (which i think is somehow related to php v7.2) during the initial setup. But i ignored it as everything worked normally as it should till now.
Hm, this error looks unrelated to the dump function... What if you comment out this dump function? Does everything work fine or do you still see this/another error? Can you show your dump function and a bit of content around it?
If i remove the dump function, everyhing works fine. But i initially got the following error and i think the problem is with the PHP v7.2 that i am running:
Warning: count(): Parameter must be an array or an object that implements Countable 500 Internal Server Error - ContextErrorException
Got this error right at the beginning after finishing the project installation and then trying to run server. Can you please help in this regard. Thanks in advance. Cheers!
OK, let's fix this problem first. Looks like you call count() function somewhere, and the value you passing to it is not an array or an object which implements Countable interface. Can you find where you call this count() function? You can debug value which is passed to count() with:
dump($someVar); die;
count($someVar);
If you still have a problem with dump(), try to use var_dump() instead.
I am not calling count() function anywhere in my code as i told you i got this error right at the beginning otherwise i would know where is the bug. Besides after researching a bit, i came to know that this is somehow related to PHP V 7.2 but i couldn't just figure out how to resolve this issue. But when i migrated to PHP V 5.6 everything worked as it should without any error (at least this one was not there anymore). I specifically got this error when i created a couple of Symfony projects, one in V 2.8.1 and another in V 3.0.1.
Hm, this sound like a bug in vendors, probably in Symfony core or in other dependencies, I found a few related issues: https://github.com/symfony/... . I think this is fixed now, so probably you just need to update dependencies to the latest versions. Anyway, it's difficult to say where's this problem exactly without debugging. If you wonder, you can follow backtrace of the calls from Symfony on the error page to see from where exactly this error came.
Hi there! I am having some trouble getting the variable being passed by my controller into the twig template. I'm returning it exactly as shown in the tutorial and PHPStorm can see it for auto completion but when I actually run the code in my browser it tells me the variable can't be found.
Oh that *is* weird - especially since PHPStorm can see the variables. Try this: remove all the variables (so that there aren't any errors anymore) and then just execute {{ dump() }}. This will dump out *all* the variables you have available to you. If there's some weird issue - you should see that your variables are *not* in this list. This isn't a solution exactly - but hopefully it'll take you to the next step of debugging.
And btw, there really *shouldn't* be anything weird happening here - there's no magic to watch out for - it's probably some tiny error somewhere :). Let me know how it goes!
I'm unable to call dump() in the twig template. I can use {{ name }} and see the array of notes in {{ notes }}, however adding {{ dump() }} anywhere in the twig file, either by itself or in addition to example code as you have shown results in a 500 error.
I have tried flushing the cache and that doesn't seem to help. Additionally, I noticed when you started typing 'dump', you had annotations pop-up. My does not do that.
I bet you use it in prod, but keep in mind that the dump() Twig function works only in dev mode. It's just for security reasons, i.e. to prevent leaking data (credentials, etc.). Actually, dump() function should be used only for debug reasons, but not for the production, where you should look over logs or use some monitoring services.
I have a vagrant box I'm using for this, so I'm not using the built in web server. I simply modified the app.php and set dev to true and it worked as expected. Thank you for pointing me in the right direction.
I've started testing my app in the production environment. It took me forever to narrow down the following error that started appearing: <br />Oops! An Error Occurred<br />The server returned a "500 Internal Server Error".<br />Something is broken. Please let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.<br />
It turned out that I had a dump function in one of the templates. <br />{{ dump() }}<br />
Actually I had the dump function enclosed in an if statement:
The documentation seems to indicate there is a dump function in the 'prod' environment, but that nothing will be dumped (it should indicate that it doesn't exist and will throw a difficlut-to-find error!): http://symfony.com/doc/current/templating/debug.html <blockquote> "The variables will only be dumped if Twig's debug setting (in config.yml) is true. By default this means that the variables will be dumped in the dev environment but not the prod environment." </blockquote>
It turns out that the dump function doesn't exist in the production (with debug=false) environment. <br />bin/console debug:twig --env=prod // no dump listed in the functions<br />
Thus, even enclosing the dump function in a check for the environment doesn't work as it still throws the non-specific error above. I'm assuming Symfony/twig's internal compiling of the template is failing when it hits the 'debug' in the 'prod' environment.
Questions: 1) Is there any logging for this type of error? When the above error was output, there were no errors written to the apache or php logs. I think this is just Symfony throwing out a '500' error on its own while fully executing as far as apache/php are concerned. Does symfony log these errors somewhere? (It was VERY difficult to debug this issue, because the issue happened in the 'prod' environment, but when I'd switch to 'dev' or 'prod'/test, everything suddenly worked, so there was nothing to debug!)
2) I'm lazy and would like to keep my 'dump' functions in my code for all time and just protect them with the 'if' statement as above. Is there a way to create a 'silent' dump function that does nothing in the 'prod' non-testing environment?
1) Yes! Production errors are logged to var/logs/prod.log. This should show the issue (it did in my case). If an error is super-fatal (which it is not in this case), you will only see error in your web server log.
2) The reason the dump() function isn't available is because the DebugBundle that provides it is not enabled in the prod environment. So yea, you could do this - and I don't see a big problem with it (to answer question #3). The simplest way would be to (A) create a Twig extension that implements a dump() function that does nothing then (B) register this as a service in config_prod.yml. We don't normally register services in that file... but this is an abnormal situation: we want that service (i.e. Twig extension) to be registered only in the prod environment, where the normal dump() doesn't exist.
Sorry it took you so long to track down! Like with all errors, if you don't know where to look, then you're totally guessing (which sucks!)
Thanks much. Knowing about the var/logs/prod.log will be very helpful and hopefully save me a lot of time in the future!
For #2 - Haven't had a chance to try it yet, but what will happen if I need to see errors in the "prod" environment and set the debug parameter to "true" in the "prod" environment. Will one dump override the other?
<br />$kernel = new AppKernel('prod', false); // no dump conflict<br />vs<br />$kernel = new AppKernel('prod', true); // twig dump conflict?<br />
BTW: In the first sentence of #2 you put "dev" but meant "prod" (for any future readers)
BTW: In the first sentence of #2 you put "dev" but meant "prod"
Thank you! I fixed that above!
If both dump() functions are enabled, one will override the other. I believe you function will win, as it's registered second - but it's not a normal situation to have two functions competing with each other. And that false versus true flag to AppKernelis what normally configures Symfony to hide or show errors. But, this flag isn't what controls the dump() function being available: it's the environment. If you look in AppKernel, the DebugBundle (which provides the function) is only enabled in the dev and test environments. If I did want to see my dumps temporarily, I would actually create a temporary new front controller with new AppKernel('dev', true) and run that - go full debug/dev mode :). But, I don't need to do this very often - the prod.log file usually has what I need (actually, we configure Monolog to send all errors to Slack, so we're basically pinged when something goes into this file) to be able to replicate something locally. But, we also never have bugs on production... ;)
They are definitely still used and relevant - check out this page: http://symfony.com/doc/curr.... The one you linked to is more of a marketing "preview" of Symfony. The link I posted is the real templating/Twig documentation in Symfony :). And it has all the details about each of those functions. We also have a reference section for all the custom things that are added to Twig by Symfony: http://symfony.com/doc/curr...
18 Comments
Hey There,
I am getting the following error while try to use dump function in the template:
[2/2] Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("Notice: A non well formed numeric value encountered") in genus\show.html.twig at line 1
[1/2] ContextErrorException: Notice: A non well formed numeric value encountered
LOGS:
CRITICAL - Uncaught PHP Exception Twig_Error_Runtime: "An exception has been thrown during the rendering of a template ("Notice: A non well formed numeric value encountered") in "genus/show.html.twig" at line 1." at C:\Data\Literature\Programming\Source Code\Symfony\Symfony 3\aqua_note\vendor\twig\twig\lib\Twig\Template.php line 396
I actually got an error of type ContextErrorException (which i think is somehow related to php v7.2) during the initial setup. But i ignored it as everything worked normally as it should till now.
Thanks in advance!
Hey Junaid,
Hm, this error looks unrelated to the dump function... What if you comment out this dump function? Does everything work fine or do you still see this/another error? Can you show your dump function and a bit of content around it?
Cheers!
Hey victor
If i remove the dump function, everyhing works fine. But i initially got the following error and i think the problem is with the PHP v7.2 that i am running:
Warning: count(): Parameter must be an array or an object that implements Countable
500 Internal Server Error - ContextErrorException
Got this error right at the beginning after finishing the project installation and then trying to run server. Can you please help in this regard. Thanks in advance. Cheers!
Yo Junaid,
OK, let's fix this problem first. Looks like you call count() function somewhere, and the value you passing to it is not an array or an object which implements Countable interface. Can you find where you call this count() function? You can debug value which is passed to count() with:
If you still have a problem with dump(), try to use var_dump() instead.
Cheers!
Hey Victor,
I am not calling count() function anywhere in my code as i told you i got this error right at the beginning otherwise i would know where is the bug. Besides after researching a bit, i came to know that this is somehow related to PHP V 7.2 but i couldn't just figure out how to resolve this issue. But when i migrated to PHP V 5.6 everything worked as it should without any error (at least this one was not there anymore). I specifically got this error when i created a couple of Symfony projects, one in V 2.8.1 and another in V 3.0.1.
Cheers!
Hey Junaid,
Hm, this sound like a bug in vendors, probably in Symfony core or in other dependencies, I found a few related issues: https://github.com/symfony/... . I think this is fixed now, so probably you just need to update dependencies to the latest versions. Anyway, it's difficult to say where's this problem exactly without debugging. If you wonder, you can follow backtrace of the calls from Symfony on the error page to see from where exactly this error came.
Cheers!
Hi there!
I am having some trouble getting the variable being passed by my controller into the twig template. I'm returning it exactly as shown in the tutorial and PHPStorm can see it for auto completion but when I actually run the code in my browser it tells me the variable can't be found.
Hi Pad!
Oh that *is* weird - especially since PHPStorm can see the variables. Try this: remove all the variables (so that there aren't any errors anymore) and then just execute {{ dump() }}. This will dump out *all* the variables you have available to you. If there's some weird issue - you should see that your variables are *not* in this list. This isn't a solution exactly - but hopefully it'll take you to the next step of debugging.
And btw, there really *shouldn't* be anything weird happening here - there's no magic to watch out for - it's probably some tiny error somewhere :). Let me know how it goes!
Hello,
I'm unable to call dump() in the twig template. I can use {{ name }} and see the array of notes in {{ notes }}, however adding {{ dump() }} anywhere in the twig file, either by itself or in addition to example code as you have shown results in a 500 error.
I have tried flushing the cache and that doesn't seem to help. Additionally, I noticed when you started typing 'dump', you had annotations pop-up. My does not do that.
Hey Michael,
I bet you use it in prod, but keep in mind that the dump() Twig function works only in dev mode. It's just for security reasons, i.e. to prevent leaking data (credentials, etc.). Actually, dump() function should be used only for debug reasons, but not for the production, where you should look over logs or use some monitoring services.
Cheers!
I have a vagrant box I'm using for this, so I'm not using the built in web server. I simply modified the app.php and set dev to true and it worked as expected. Thank you for pointing me in the right direction.
I've started testing my app in the production environment. It took me forever to narrow down the following error that started appearing:
<br />Oops! An Error Occurred<br />The server returned a "500 Internal Server Error".<br />Something is broken. Please let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.<br />It turned out that I had a dump function in one of the templates.
<br />{{ dump() }}<br />Actually I had the dump function enclosed in an if statement:
The documentation seems to indicate there is a dump function in the 'prod' environment, but that nothing will be dumped (it should indicate that it doesn't exist and will throw a difficlut-to-find error!):
http://symfony.com/doc/current/templating/debug.html
<blockquote>
"The variables will only be dumped if Twig's debug setting (in config.yml) is true. By default this means that the variables will be dumped in the dev environment but not the prod environment."
</blockquote>
It turns out that the dump function doesn't exist in the production (with debug=false) environment.
<br />bin/console debug:twig --env=prod // no dump listed in the functions<br />Thus, even enclosing the dump function in a check for the environment doesn't work as it still throws the non-specific error above. I'm assuming Symfony/twig's internal compiling of the template is failing when it hits the 'debug' in the 'prod' environment.
Questions:
1) Is there any logging for this type of error? When the above error was output, there were no errors written to the apache or php logs. I think this is just Symfony throwing out a '500' error on its own while fully executing as far as apache/php are concerned. Does symfony log these errors somewhere? (It was VERY difficult to debug this issue, because the issue happened in the 'prod' environment, but when I'd switch to 'dev' or 'prod'/test, everything suddenly worked, so there was nothing to debug!)
2) I'm lazy and would like to keep my 'dump' functions in my code for all time and just protect them with the 'if' statement as above. Is there a way to create a 'silent' dump function that does nothing in the 'prod' non-testing environment?
3) Is #2 bad practice?
Hi Terry!
Wow, you're right! This is absolutely a bug in the documentation - I've just verified the same behavior as you: https://github.com/symfony/symfony-docs/issues/7937
Now, let's get to your questions:
1) Yes! Production errors are logged to var/logs/prod.log. This should show the issue (it did in my case). If an error is super-fatal (which it is not in this case), you will only see error in your web server log.
2) The reason the
dump()function isn't available is because the DebugBundle that provides it is not enabled in the prod environment. So yea, you could do this - and I don't see a big problem with it (to answer question #3). The simplest way would be to (A) create a Twig extension that implements a dump() function that does nothing then (B) register this as a service inconfig_prod.yml. We don't normally register services in that file... but this is an abnormal situation: we want that service (i.e. Twig extension) to be registered only in the prod environment, where the normal dump() doesn't exist.Sorry it took you so long to track down! Like with all errors, if you don't know where to look, then you're totally guessing (which sucks!)
Cheers!
Thanks much. Knowing about the var/logs/prod.log will be very helpful and hopefully save me a lot of time in the future!
For #2 - Haven't had a chance to try it yet, but what will happen if I need to see errors in the "prod" environment and set the debug parameter to "true" in the "prod" environment. Will one dump override the other?
<br />$kernel = new AppKernel('prod', false); // no dump conflict<br />vs<br />$kernel = new AppKernel('prod', true); // twig dump conflict?<br />BTW: In the first sentence of #2 you put "dev" but meant "prod" (for any future readers)
Yo Terry Caliendo!
Thank you! I fixed that above!
If both dump() functions are enabled, one will override the other. I believe you function will win, as it's registered second - but it's not a normal situation to have two functions competing with each other. And that
falseversustrueflag toAppKernelis what normally configures Symfony to hide or show errors. But, this flag isn't what controls thedump()function being available: it's the environment. If you look in AppKernel, the DebugBundle (which provides the function) is only enabled in the dev and test environments. If I did want to see my dumps temporarily, I would actually create a temporary new front controller withnew AppKernel('dev', true)and run that - go full debug/dev mode :). But, I don't need to do this very often - the prod.log file usually has what I need (actually, we configure Monolog to send all errors to Slack, so we're basically pinged when something goes into this file) to be able to replicate something locally. But, we also never have bugs on production... ;)Cheers!
Hi Ryan,
I read the Docs from:
http://symfony.com/doc/curr...
and I can't find the docs for: path(), url(), and asset() function in Twig docs or somethere else.
are these depreciated?
Thanks for your advice
Teo
Hey Teo!
They are definitely still used and relevant - check out this page: http://symfony.com/doc/curr.... The one you linked to is more of a marketing "preview" of Symfony. The link I posted is the real templating/Twig documentation in Symfony :). And it has all the details about each of those functions. We also have a reference section for all the custom things that are added to Twig by Symfony: http://symfony.com/doc/curr...
I hope that helps! Cheers!
Yep, Ryan I see now
Thank you very much!
Cheers,
Teo
"Houston: no signs of life"
Start the conversation!