如何在Magento 2中打开错误信息

23

我已经安装了Magento 2并且它正在工作。我刚刚创建了Hello world模块,它现在也在工作。

我的问题是,在execute方法中调用了不存在的方法。

当我尝试加载页面时,它显示一个空白白屏,但没有错误信息。

如何在Magento 2中显示错误信息?


你到目前为止尝试了什么?展示你的代码!请编辑你的问题! - jogo
使用CLI命令php bin/magento deploy:mode:set developer启用开发者模式,如此处所示。 - Steve Johnson
也要检查Web服务器的error_log。在托管环境中,通常会写入到~/的子文件夹中。 - Krista K
7个回答

41
在index.php文件中添加以下代码:-
error_reporting(E_ALL);
ini_set('display_errors', 1);

1
设置开发者模式不会显示致命错误。在我的情况下,我看到了一个白色的页面。这个修复了 :) 谢谢。 - l00k
对于初始白页错误(预Magento / PHP),这确实提供了更好的结果。 - Rock_Artist
这已经包含在 app/bootstrap.php 中作为一行注释的代码。也可以在那里删除注释... - feeela
首先,检查您的 php.ini 文件。如果 error_reportingdisplay_errors 被关闭了,您应该在那里进行修复,而不是在 bootstrap.php 中进行修复。否则,您最终会在生产环境中显示错误,并可能泄漏敏感数据。 - stollr
有一些最新的博客可供查阅,网址为https://www.codedecorator.com/blog/how-to-enable-error-log-in-magento2-4/。 - Bhupendra Jadeja

38
在Magento2中启用错误报告有一点棘手,因为Magento2现在有三种不同的模式:
1. 默认模式 2. 开发者模式 3. 生产模式
Magento2默认安装在“默认”模式下,所以您在网站前端不会得到错误日志,可以在Magento错误日志中查看完整的详细信息。您可以在此处检查所有详细信息:http://devdocs.magento.com/guides/v2.0/config-guide/bootstrap/magento-modes.html 如何在Magento2中启用开发者模式
如果您是Magento2的开发人员并创建扩展和模板,则必须拥有此功能。Magento为此提供了一个命令。登录到Linux终端并在Magento下执行该命令。
php bin/magento deploy:mode:set developer

这将在您的Magento实例下启用开发者模式,对于生产模式,您可以将模式设置为“生产”或“默认”。

如果仍然遇到错误报告问题,

您可以尝试将pub/errors下的local.xml.sample重命名为local.xml


14

这是一个适合这种情况的地方。在bootstrap.php文件中,有以下代码:

#ini_set('display_errors', 1);

只需将其注释取消。


8

1) app/bootstrap.php,其中有:

#ini_set('display_errors', 1);

请取消注释。

2) 运行以下命令

php bin/magento deploy:mode:set developer
php bin/magento cache:clean
php bin/magento cache:flush

php bin/magento cache:clean is a subset of php bin/magento cache:flush, in other workds you only need run the cache:flush - Barry

3

要检查当前模式,请使用:

bin/magento deploy:mode:show

那不是问题。 - Black
有时候我们应该从更广的角度看待问题,这也是问题范围内的一部分。 - shustr8

2

除了编辑index.phpapp/bootstrap.php文件外,您还可以编辑.htaccess文件以启用PHP显示错误设置。

在Magento2网站根目录中的.htaccess文件末尾编写以下代码行:

## enable PHP's error display settings
php_value display_errors on
## set error display to E_ALL
php_value error_reporting -1

之后,
  • Open terminal
  • Go to your Magento's root directory

    cd /path/to/your/magento/root/folder
    
  • Run the following command to enable developer mode:

    php bin/magento deploy:mode:set developer
    
  • To check your current developer mode, you can run the command:

    php bin/magento deploy:mode:show
    

1
请将以下代码放入index.php文件中。
   ini_set('error_reporting', E_ERROR);
   register_shutdown_function("fatal_handler");
   function fatal_handler() {
     $error = error_get_last();
     echo("<pre>");
     print_r($error);
   }

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