如何在Zend Framework 2中打开PHP错误报告?

9

每当我在Zend Framework 2中收到错误时,我只会看到500个内部服务器错误的显示,并且必须在Zend Server错误日志中搜索。

我尝试将以下代码添加到config/autoload/local.php文件中,但没有效果:

return array(
    'phpSettings' => array(
        'display_startup_errors' => true,
        'display_errors' => true,
        ),
);
2个回答

8

在zf2中没有原生支持此功能(据我所知)。您需要将它们设置在php.ini本身中,或者在index.php中设置它们。

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

如果你真的想将它们作为配置设置提供,你可以保留目前代码,然后在模块引导中获取它们,并通过每个键值对调用ini_set()函数。
public function onBootstrap(EventInterface $e) {
    $app = $e->getApplication();
    $sm = $app->getServiceManager();
    $config = $sm->get('Config');
    $phpSettings = isset($config['phpSettings']) ? $config['phpSettings'] : array();
    if(!empty($phpSettings)) {
        foreach($phpSettings as $key => $value) {
            ini_set($key, $value);
        }
    }
}

编辑:正如@akond在评论中正确指出的那样,您可以将ini_set行添加到local.php中,这是更好的解决方案。


3
没有必要使用这么长的OnBoostrap代码。他可以直接将error_reporting放在配置文件中,因为它是一个PHP文件。 - akond
@akond +1 你说得很对,那是更好的选择,引导代码只是为了演示如何使OP的工作正常。 - Crisp
1
如果您将这些行放在local.php中,可能会错过在尝试加载此文件之前发生的错误。我发现我必须将这些行放在index.php中。 - Caspar Harmer

6

如果您想在ZF2应用程序中轻松配置phpSettings,您应考虑使用DluPhpSettings

使用此模块,您可以为每个环境配置设置:

/* Local application configuration in /config/autoload/phpsettings.local.php */
<?php
return array(
    'phpSettings'   => array(
        'display_startup_errors'        => false,
        'display_errors'                => false,
        'max_execution_time'            => 60,
        'date.timezone'                 => 'Europe/Prague',
        'mbstring.internal_encoding'    => 'UTF-8',
    ),
);

查看这篇博客文章以获取更多信息!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接