Using Laravel 5.5.34, I have trouble outputting debug information in Blade templates using the dump() helper.
{{ dump('test') }}
results in the following output:
I wouldn’t expect the raw string “test” to show up below the actual debug output. Is this the normal behavior and if yes, how can I disable it? If no, what misconfiguration could cause it?
Advertisement
Answer
Digging a little deeper, I found the source of the problem.
First of all, Blade translates this…
{{ dump($var) }}
to this:
<?php echo e(dump($var)); ?>
That has always worked fine, because Symfony’s dump() helper has never returned a value. However, they have changed that with this commit: https://github.com/symfony/var-dumper/commit/b6d0c8cd9949a5de4e71413e6ffbc2ea9dcb647f#diff-2e42573e053ced723652b17a395226f0
Since then, dump() does return $var
!
Because Laravel uses this aforementioned dump() helper from symfony/var-dumper, e() will now suddenly receive $var
back from dump().
This is the e() helper function used in Laravel:
function e($value, $doubleEncode = false) { if ($value instanceof Htmlable) { return $value->toHtml(); } return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', $doubleEncode); }
This causes the double output in case $var
is of type string, and throws an exception if it isn’t, because htmlspecialchars() only accepts strings as first argument.
The solution was to create my own dump helper that doesn’t contain the return statement you can see in the diff.