Reputation: 1749
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?
Upvotes: 4
Views: 2911
Reputation: 3992
Another option is to use symfony/var-exporter
composer require symfony/var-exporter
$string = VarExporter::export($yourVariableOrArray);
https://github.com/symfony/var-exporter
Upvotes: 2
Reputation: 1749
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.
Upvotes: 6
Reputation: 7764
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.
Upvotes: 0