我使用composer require安装了VarDumper。我已经在我的控制器中调用了dump()函数,这应该能正常工作吗?
composer require symfony/var-dumper-
public function indexAction()
{
$messages = Array(
'x' => 'y',
'a' => 'b',
'c' => 'd'
);
dump($messages);
}这就是我所犯的错误。但为什么我不能叫我的控制器转储呢?
Attempted to call function "dump" from namespace "App\Bundle\Controller".发布于 2015-05-26 14:55:48
开发类环境
对于类似开发的环境(开发、测试等),必须确保在应用程序内核中启用了DebugBundle:
DebugBundle在Symfony应用程序中集成了VarDumper组件。所有这些选项都是在应用程序配置中的调试键下配置的。
溶液
// 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();
// ...
}
}
// ...
}生产类环境
这是一个在生产环境中转储变量的坏实践,但是有些情况不适合最佳实践。
根据环境的不同,全局函数dump()可能有多个声明(在pear/XML、pear/adobd等中)。此外,如果仔细查看新的Symfony dump()函数声明,只有在它不存在的情况下才会创建它:
if (!function_exists('dump')) {
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
function dump($var)
{
foreach (func_get_args() as $var) {
VarDumper::dump($var);
}
}
}溶液
因此,好的解决方案是从名称空间VarDumper::dump()直接调用Symfony\Component\VarDumper\VarDumper。我还建议将其封装在exit()中以避免意外行为:
use Symfony\Component\VarDumper\VarDumper;
class myClass
{
function myFunction()
{
exit(VarDumper::dump(...));
}
}发布于 2015-10-22 13:26:17
确保在应用程序的内核中启用了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();
// ...
}
}
// ...
}发布于 2015-05-12 13:33:18
composer全局需要symfony/var-dumper
您将看到:已更改的当前目录(GLOBAL_COMPOSER_DIRECTORY)
在您的(GLOBAL_COMPOSER_DIRECTORY)/vendor/autoload.php : auto_prepend_file =auto_prepend_file中
然后,您可以在所有项目中使用转储,而不必安装它。
https://stackoverflow.com/questions/29984955
复制相似问题