5

I am writing an API in Symfony 3, I would like to use the Symfony VarDumper dump() function. However dump() is outputting HTML which is rather annoying in my situation. (My client for calling the API does not render HTML)

I would rather the output be plain text, JSON, Yaml, or at least the output that would be used by dump() when it is in CLI mode.

What would be a good way to do this?

3
  • Is this a trick question? Use the json serializer functionality to output json. Commented Aug 9, 2016 at 21:02
  • When I am developing or debugging, sometime there is a variable, and this variable could be any kind value or object. I like to be able to quickly use dump() to get an idea about the variable and the type of object it is etc. I might use dump() on a ArrayCollections, form type, service etc. Perhaps you miss understood my intentions? Are you suggesting I use json_encode()? Surly this would not be able to output complex nested objects for debugging, including exposing private properties of objects? Commented Aug 9, 2016 at 21:40
  • Indeed I thought you wanted to use dumper as part of your api. I have used it inside of console commands as well as phpunit test and I get readable output. Instead of using the wrapper dump function you might try VarDumper::dump() and see what happens. Commented Aug 9, 2016 at 22:03

3 Answers 3

6

I was able to use the following code to get dump() to output to text/cli format instead of HTML by doing this

use Symfony\Component\VarDumper\Cloner\VarCloner; use Symfony\Component\VarDumper\Dumper\CliDumper; use Symfony\Component\VarDumper\VarDumper; //................ CliDumper::$defaultOutput = 'php://output'; VarDumper::setHandler(function ($var) { $cloner = new VarCloner(); $dumper = new CliDumper(); $dumper->dump($cloner->cloneVar($var)); }); dump($debugMe); 

I may also mention, that I would discourage the use of print_r() for var_dump() for debugging purposes in Symfony applications, often the output is a little more confusing and cluttered, and for what ever reason print_r() does not even work in some occasions.

Sign up to request clarification or add additional context in comments.

Comments

2

Another option is to use symfony/var-exporter

composer require symfony/var-exporter 
$string = VarExporter::export($yourVariableOrArray); 

https://github.com/symfony/var-exporter

Comments

0

Let's say you've rendered in your controller to a twig file like so:

return $this->render('admin/maintain_divisions.html.twig', array( 'form' => $form->createView(), 'var' => $variable, )); 

Where var is the varaible; Then in the twig file you would use:

{{ dump(var) }} 

The variable could be anything, even a doctrine result set.

Within a controller you can use:

var_dump($variable); 

This is a PHP function.

2 Comments

Thanks for the advice. Previously I was using "raulfraile/ladybug-bundle" ldd() function (configured to output text, not HTML), the output is much more readable that var_dump(), in some cases var_dump() output is almost unreadable. print_r() seems to hit infinite loop or something and execution never completes. I am switching to VarDumper as ladybug doesn't seem so supported anymore and currently it is conflicting with a composer dependency. Presently I am not using twig anywhere in this application but thanks for pointing out {{ dump() }}
Yes, you are correct Sam, you should use print_f() for arrays. I forgot about that. If it loops, definitely something is wrong. I highly suggest using TWIG files (only in the DEV environment) to dump. If it's an array, you'll see all the details when dumped in TWIG. If you think I helped, you should do the 'upvote' for my answer. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.