📜 ⬆️ ⬇️

VarDumper - a new component in symfony 2.6

A few days ago there was a release of Symfony 2.6.0 , which contains many interesting innovations. One of the most important is the VarDumper component.

VarDumper is designed to replace the well-known var_dump() function var_dump() more modern and functional alternative - the dump() function.

Before using it, make sure that DebugBundle connected in AppKernel :

 // app/AppKernel.php use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Config\Loader\LoaderInterface; class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... ); if (in_array($this->getEnvironment(), array('dev', 'test'))) { $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle(); // ... } } // ... } 

')
Now you can replace all calls to the var_dump() function with a new and shorter dump() . Unlike var_dump() , you can safely use dump() to display the contents of any variables, including variables with circular references, such as Doctrine entities.

Consider, for example, a controller that displays the entire symfony container and the Request object:

 namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class DefaultController extends Controller { public function indexAction(Request $request) { dump($this->container, $request); // ... } } 


When this controller is displayed in the browser, you will see a new dump panel that records all the requested variables and briefly shows their contents:



When you click on the panel, you will receive the full contents of these variables, including information about links, public / protected / private properties and methods, unlimited nesting, etc .:



In addition to integrating with the symfony debug panel, the component is smart enough to detect whether you use exit or die() in your code. In these cases, the variables are written to the standard output.

VarDumper also contains the {% dump %} tag and the {{ dump() }} function for checking variables directly from Twig templates. The {% dump %} tag shows variables in the debug panel (for example, {% dump variable1, variable2 %} ). A great option when the output of the template should not be changed.

The {{ dump() }} function, on the contrary, displays the contents of variables directly in the template (for example, {{ dump(variable1, variable2) }} ).

The VarDumper component comes with symfony starting from version 2.6.0, but if some of your projects are stuck on versions 2.3, 2.4 or 2.5, you can install the debug-bundle as follows:

 $ composer require --dev tchwork/debug-bundle 


You can read more in the documentation of the VarDumper component , as well as the article Advanced use of the VarDumper component .

Source: https://habr.com/ru/post/245623/


All Articles