Skip to content

Commit ee2d7e4

Browse files
section for DebugBundle/Twig integration
1 parent b5b45bc commit ee2d7e4

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

components/var_dumper/advanced.rst

+12
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ For example, to get a dump as a string in a variable, you can do::
7777

7878
// $output is now populated with the dump representation of $variable
7979

80+
An other option for doing the same could be::
81+
82+
$output = fopen('php://memory', 'r+b');
83+
cloner = new VarCloner();
84+
$dumper = new CliDumper($output);
85+
86+
$dumper->dump($cloner->cloneVar($variable));
87+
fseek($output, 0);
88+
$output = stream_get_contents($output);
89+
90+
// $output is now populated with the dump representation of $variable
91+
8092
Dumpers implement the :class:`Symfony\\Component\\VarDumper\\Dumper\\DataDumperInterface`
8193
interface that specifies the
8294
:method:`dump(Data $data) <Symfony\\Component\\VarDumper\\Dumper\\DataDumperInterface::dump>`

components/var_dumper/introduction.rst

+47-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,52 @@ The advantages of this function are:
4545
so can you also use it directly.
4646
You can change the behavior of this function by calling
4747
:method:`VarDumper::setHandler($callable) <Symfony\\Component\\VarDumper\\VarDumper::setHandler>`:
48-
calls to ``dump()`` will then be forwarded to ``$callable``, given as first argument.
48+
calls to ``dump()`` will then be forwarded to ``$callable``.
49+
50+
Where does the output go?
51+
-------------------------
52+
53+
If you read the advanced documentation, you'll learn how to change the
54+
format or redirect the output to wherever you want.
55+
56+
By default, these are selected based on your current PHP SAPI:
57+
58+
- on the command line (CLI SAPI), the output is written on `STDERR`. This
59+
can be surprising to some because this bypasses PHP's output buffering
60+
mechanism. On the other hand, this give the possibility to easily split
61+
dumps from regular output by using pipe redirection.
62+
- on other SAPIs, dumps are written as HTML on the regular output.
63+
64+
DebugBundle and Twig integration
65+
--------------------------------
66+
67+
The `DebugBundle` allows greater integration of the component into the
68+
Symfony full stack framework. It is enabled by default in the dev
69+
environement of the standard edition since version 2.6.
70+
71+
Since generating (even debug) output in the controller or in the model
72+
of your application may just break it by e.g. sending HTTP headers or
73+
corrupting your view, the bundle configures the `dump()` function so that
74+
variables are dumped in the web debug toolbar.
75+
76+
But if the toolbar can not be displayed because you e.g. called `die`/`exit`
77+
or a fatal error occurred, then dumps are written on the regular output.
78+
79+
In a Twig template, two constructs are available for dumping a variable.
80+
Choosing between both is generally only a matter of personal taste:
81+
82+
- `{% dump foo.bar %}` is the way to go when the original template output
83+
shall not be modified: variables are not dumped inline, but in the web
84+
debug toolbar.
85+
- on the contrary, `{{ dump(foo.bar) }}` dumps inline and thus may or not
86+
be suited to your use case (e.g. you shouldn't use it in an HTML
87+
attribute or a `script` tag).
88+
89+
Reading a dump
90+
--------------
91+
92+
For simple variables, reading the output should be straightforward::
93+
94+
dump(array(true, 1.1, "string"));
4995

5096
.. _Packagist: https://packagist.org/packages/symfony/var-dumper

0 commit comments

Comments
 (0)