为什么我的VarDumper无法在Symfony2中运行

时间:2022-10-16 13:41:41

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

我已经使用composer require安装了VarDumper。我在我的控制器中调用了dump()函数,这应该可行吗?

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?

这是我得到的错误。但为什么我不能在我的控制器中调用dump?

Attempted to call function "dump" from namespace "App\Bundle\Controller".

4 个解决方案

#1


17  

Depending on the environment, there may be multiple declarations of the global function dump() (i.e. in pear/XML, pear/adobd, etc).

根据环境的不同,可能会有多个全局函数dump()声明(即pear / XML,pear / adobd等)。

Moreover if you look closely at the new Symfony dump function declaration, it is created only if it doesn't already exist :

此外,如果仔细查看新的Symfony转储函数声明,只有在它尚不存在时才会创建它:

if (!function_exists('dump')) {
    /**
     * @author Nicolas Grekas <p@tchwork.com>
     */
    function dump($var)
    {
        foreach (func_get_args() as $var) {
            VarDumper::dump($var);
        }
    }
}

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 :

因此,好的解决方案是直接从命名空间Symfony \ Component \ VarDumper \ VarDumper调用VarDumper :: dump()。我还建议将其包装在exit()中以避免意外行为:

use Symfony\Component\VarDumper\VarDumper;

class myClass
{
    function myFunction()
    {
        exit(VarDumper::dump(...));
    }
}

#2


21  

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

确保在应用程序的内核中启用了DebugBundle包

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

    // ...
}

#3


0  

composer global require symfony/var-dumper

composer global需要symfony / var-dumper

You will see : Changed current directory (GLOBAL_COMPOSER_DIRECTORY)

您将看到:更改当前目录(GLOBAL_COMPOSER_DIRECTORY)

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

在你的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

然后,您可以在所有项目中使用dump而无需安装它

#4


-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.

尝试使用命令php composer.phar update更新项目的依赖项。必须在编写器需要symfony / var-dumper之后运行该命令。

#1


17  

Depending on the environment, there may be multiple declarations of the global function dump() (i.e. in pear/XML, pear/adobd, etc).

根据环境的不同,可能会有多个全局函数dump()声明(即pear / XML,pear / adobd等)。

Moreover if you look closely at the new Symfony dump function declaration, it is created only if it doesn't already exist :

此外,如果仔细查看新的Symfony转储函数声明,只有在它尚不存在时才会创建它:

if (!function_exists('dump')) {
    /**
     * @author Nicolas Grekas <p@tchwork.com>
     */
    function dump($var)
    {
        foreach (func_get_args() as $var) {
            VarDumper::dump($var);
        }
    }
}

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 :

因此,好的解决方案是直接从命名空间Symfony \ Component \ VarDumper \ VarDumper调用VarDumper :: dump()。我还建议将其包装在exit()中以避免意外行为:

use Symfony\Component\VarDumper\VarDumper;

class myClass
{
    function myFunction()
    {
        exit(VarDumper::dump(...));
    }
}

#2


21  

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

确保在应用程序的内核中启用了DebugBundle包

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

    // ...
}

#3


0  

composer global require symfony/var-dumper

composer global需要symfony / var-dumper

You will see : Changed current directory (GLOBAL_COMPOSER_DIRECTORY)

您将看到:更改当前目录(GLOBAL_COMPOSER_DIRECTORY)

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

在你的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

然后,您可以在所有项目中使用dump而无需安装它

#4


-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.

尝试使用命令php composer.phar update更新项目的依赖项。必须在编写器需要symfony / var-dumper之后运行该命令。