12

I've installed VarDumper using composer require. I've called the dump() function in my controller, this should work right?

composer require symfony/var-dumper

-

public function indexAction()
{
    $messages = Array(
            'x' => 'y',
            'a' => 'b',
            'c' => 'd'
            );

    dump($messages);
}

This is the error I get. But why can't I call dump in my controller?

Attempted to call function "dump" from namespace "App\Bundle\Controller".
1
  • What version is your installation of Symfony? What does your controller declaration look like?
    – sjagr
    May 4, 2015 at 13:56

4 Answers 4

22

Development-like environments

Whithin a development-like envionment (development, test, etc), you have to make sure the DebugBundle is enabled in the application kernel:

The DebugBundle integrates the VarDumper component in Symfony applications. All these options are configured under the debug key in your application configuration.

Solution

// 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();
            // ...
        }
    }

    // ...
}

Production-like environments

This is a bad practice to dump variables in a production environment but some situations can't fit best practices.

Depending on the environment, there may be multiple declarations of the global function dump() (in pear/XML, pear/adobd, etc). Moreover if you look closely at the new Symfony dump() function declaration, it is created only if it doesn't already exist:

if (!function_exists('dump')) {
    /**
     * @author Nicolas Grekas <[email protected]>
     */
    function dump($var)
    {
        foreach (func_get_args() as $var) {
            VarDumper::dump($var);
        }
    }
}

Solution

So the good solution is to directly call VarDumper::dump() from the namespace Symfony\Component\VarDumper\VarDumper. I also suggest to wrap it inside an exit() to avoid unexpected behaviours:

use Symfony\Component\VarDumper\VarDumper;

class myClass
{
    function myFunction()
    {
        exit(VarDumper::dump(...));
    }
}
1
  • 1
    Legend! It worked using the 2nd option you provided. Very helpful, thanks.
    – anche
    May 27, 2015 at 15:00
22

Make sure that the DebugBundle bundle is enabled in the application's kernel

// 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();
            // ...
       }
    }

    // ...
}
2
  • 2
    Thanks, when upgrading from 2.3 I was missing that Bundle.
    – Alsciende
    Nov 5, 2015 at 13:57
  • 4
    Actually this answer is the correct one. Chosen answer allows you to make it work, but this one makes using dump() function directly Jun 28, 2016 at 10:10
0

composer global require symfony/var-dumper

You will see : Changed current directory (GLOBAL_COMPOSER_DIRECTORY)

In your php.ini: auto_prepend_file = (GLOBAL_COMPOSER_DIRECTORY)/vendor/autoload.php

You can then use dump in all your projects without having to install it

2
  • not really working so far. Do I need to include them in an use statement in my symfony project?
    – anche
    May 15, 2015 at 15:23
  • You have to make sure that the modified php.ini is being used by your php stack. In command-line: php --ini In apache: <?php phpinfo(); May 26, 2015 at 15:24
-2

Try to update your project's dependencies with the command php composer.phar update. That command must be run after the composer require symfony/var-dumper.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.