Codeigniter + Whoops

2019-04-13 16:36发布

问题:

I'm trying to setup Whoops on a Codeigniter 3 application.

I installed Whoops with composer and calling it like this :

use Whoops\Handler\PrettyPageHandler;
if (ENVIRONMENT == 'development') {
    $whoops = new \Whoops\Run;
    $whoops->pushHandler(new Whoops\Handler\PrettyPageHandler());
    $whoops->register();

    $handler = new PrettyPageHandler;
    $handler->setEditor('sublime');
}

It works for warnings, notices and deprecated errors, but not for fatal errors.

CodeIgniter seems to handle them before Whoops. Is there a way to modify this behavior?

回答1:

I got it working by moving the "Whoops Run" in my index.php file, right after defining my development environment and error reporting settings (around line 70 in the CI index.php file) :

/*
 *---------------------------------------------------------------
 * ERROR REPORTING
 *---------------------------------------------------------------
 *
 * Different environments will require different levels of error reporting.
 * By default development will show errors but testing and live will hide them.
 * 2017-08-21: Adding Whoops profiler
 */
use Whoops\Handler\PrettyPageHandler;
switch (ENVIRONMENT)
{
    case 'development':
    case 'staging':
        error_reporting(-1);
        ini_set('display_errors', 1);
        error_reporting(E_ALL ^ E_WARNING ^ E_USER_WARNING ^ E_NOTICE ^ E_DEPRECATED );
        $whoops = new \Whoops\Run;
        $whoops->pushHandler(new Whoops\Handler\PrettyPageHandler());
        $whoops->register();
    break;

    case 'testing':
    case 'production':
        ini_set('display_errors', 0);
        if (version_compare(PHP_VERSION, '5.3', '>='))
        {
            error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
        }
        else
        {
            error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE);
        }
    break;

    default:
        header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
        echo 'The application environment is not set correctly.';
        exit(1); // EXIT_ERROR
}

A comment in this Github Whoops issue tipped me off!