php - 如何使用 Laravel 中的 $dontReport 选项禁用 ErrorException 显示

标签 php laravel exception laravel-5.4

标题很容易解释:如何确保正常的 PHP 错误(例如尝试输出 undefined variable )。

echo $a; // ErrorException: Undefined variable: a

在 PHP 中,这只是一个错误,但是在 laravel 中,错误会升级为 ErrorException,从而停止执行。我怎样才能禁用这个功能。我试过了

app/Exceptions/Handler.php

  protected $dontReport = [
        \Illuminate\Auth\AuthenticationException::class,
        \Illuminate\Auth\Access\AuthorizationException::class,
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
        \Illuminate\Session\TokenMismatchException::class,
        \Illuminate\Validation\ValidationException::class,
        \Illuminate\Exception\ErrorException::class, // added this
        \ErrorException::class, // and this
    ];

此问题与 my earlier question 相关,但我想到使用另一种可能效果更好的方法。

我试图实现的是,就像错误一样,代码不会停止执行,而是继续尽可能地运行。

最佳答案

Laravel 使用它自己的自定义错误处理程序。根据PHP manual :

The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE(this type of error probably), E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.

因此 Laravel 无法捕获并处理此类错误。

当脚本停止执行并且 shutdown function 时,错误会转换为 ErrorException叫做。那时你无能为力。

关于php - 如何使用 Laravel 中的 $dontReport 选项禁用 ErrorException 显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45608638/

相关文章:

php - 仅从 for 循环中的第二项添加显示属性

php - 如何使用PHP连接数据库MySql?

java - 使用 throws 关键字并在 main 中处理它,而不是在它发生的方法中处理它

c++ - Windows默默捕捉到的异常,如何手动处理?

php - 使用一列中的多个值规范化 MySQL 数据库

php - Laravel 架构构建器 dateTimeTz 示例看起来如何?

php - 从逗号分隔列表中添加或删除项目

laravel - 从资源文件夹中获取图像 - Laravel

c - 使用strncpy复制字符串时遇到异常

php - 无法列出 sql 到 html 表的结果