New in Symfony 4.1: VarDumper server
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
Contributed by
Maxime Steinhausser
in #23831.
The VarDumper component provides a dump()
function as a more advanced
alternative to PHP's var_dump()
function. The problem of dumping data from
your application is that, for example, when working on an API you might end up
in the console with a mix of your response data and the dumped data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class ApiController extends AbstractController
{
/**
* @Route("/hello")
*/
public function hello(Request $request, UserInterface $user)
{
dump($request->attributes, $user);
return JsonResponse::create([
'status' => 'OK',
'message' => "Hello {$user->getUsername()}"
]);
}
}
In this case, the console output is confusing:
In order to solve these issues, in Symfony 4.1 we've introduced a dedicated
server to collect the dumped data. In practice you just need to run the new
server:dump
command and whenever you call to dump()
, the dumped data
is sent to a single centralized server that outputs it to the console or to a
file in HTML format:
1 2 3 4 5 6
# displays the dumped data in the console:
$ ./bin/console server:dump
[OK] Server listening on tcp://0.0.0.0:9912
# stores the dumped data in a file using the HTML format:
$ ./bin/console server:dump --format=html > dump.html
This is how the server looks when dumping contents to the console (which includes context information such as the source file, the HTTP request, the executed command, etc.):
And this is how it looks when using the HTML format:
When using it inside a Symfony application, the new server is configured in the
debug
package:
1 2 3
# config/packages/dev/debug.yaml
debug:
dump_destination: "tcp://%env(VAR_DUMPER_SERVER)%"
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
$> VAR_DUMPER_SERVER="127.0.0.1:9913" bin/console server:dump
and for the server dumper to send Data clones to the right server, start the php web server with the same env var value:
$> VAR_DUMPER_SERVER="127.0.0.1:9913" bin/console server:run
But actually, the idea of the env var is more to help centralizing dumps to a single server instance when working with multiple apps, so you have only this env var to tweak globally.
A PR is also pending to make registration of the ServerDumper easy: https://github.com/symfony/symfony/pull/26695