Php 为什么我的Vardumpper不能在Symfony2中工作
我已经使用composer require安装了VarDumper。我已经在我的控制器中调用了dump()函数,应该可以正常工作了吧Php 为什么我的Vardumpper不能在Symfony2中工作,php,symfony,Php,Symfony,我已经使用composer require安装了VarDumper。我已经在我的控制器中调用了dump()函数,应该可以正常工作了吧 composer require symfony/var-dumper - 这就是我得到的错误。但为什么我不能在控制器中调用dump Attempted to call function "dump" from namespace "App\Bundle\Controller". 尝试使用命令php composer.phar update更新项目的依赖项。该
composer require symfony/var-dumper
-
这就是我得到的错误。但为什么我不能在控制器中调用dump
Attempted to call function "dump" from namespace "App\Bundle\Controller".
尝试使用命令
php composer.phar update
更新项目的依赖项。该命令必须在composer require symfony/var转储程序
composer全局require symfony/var转储程序之后运行
您将看到:已更改的当前目录(全局\u编写器\u目录)
在php.ini中:
auto_prepend_file=(全局_COMPOSER_目录)/vendor/autoload.php
然后,您可以在所有项目中使用dump,而无需在类似开发的环境中安装它
在开发环境(开发、测试等)中,必须确保在应用程序内核中启用:
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
等中)可能有多个声明。此外,如果仔细查看新的Symfonydump()
函数,则仅当它不存在时才会创建它:
if (!function_exists('dump')) {
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
function dump($var)
{
foreach (func_get_args() as $var) {
VarDumper::dump($var);
}
}
}
确保在应用程序的内核中启用了DebugBundlebundle
// 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();
// ...
}
}
// ...
}
不是真的@你说得对。可能OP在初始命令中缺少
php
?您安装的Symfony是什么版本?你的控制器声明是什么样子的?到目前为止还没有真正起作用。我需要在我的symfony项目的use语句中包含它们吗?您必须确保修改后的php.ini正在被php堆栈使用。在命令行中:php--ini在apache:Legend中!它使用您提供的第二个选项工作。非常有帮助,谢谢。谢谢,从2.3升级时,我错过了这个捆绑包。事实上,这个答案是正确的。选择的答案可以让它工作,但这个答案可以直接使用dump()函数
use Symfony\Component\VarDumper\VarDumper;
class myClass
{
function myFunction()
{
exit(VarDumper::dump(...));
}
}
// 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();
// ...
}
}
// ...
}