Symfony News

New in Symfony 4.1: VarDumper server

Maxime Steinhausser

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)%"

Be trained by Symfony experts - 2018-04-3 Paris - 2018-04-3 Paris - 2018-04-3 Paris


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook