PHP whoops error on slim framework
Install the composer
curl -sS https://getcomposer.org/installer | php
Edit composer.json
Slim | Whoops | Version | Global Mode | PHP DI |
---|---|---|---|---|
1 | n/a | 0.1.* | no | no |
2 | 1.* | 0.3.* | no | no |
3 | <= 1.* | 0.4.* | no | no |
3 | >= 2.* | 0.5.* | no | no |
3 | >= 2.* | 0.6.* | yes | yes |
For Slim framework 3
, The composer.json
will looks like
{
"require": {
"zeuxisoo/slim-whoops": "0.6.*"
}
}
Now, install
or update
the dependencies
php composer.phar install
Just need to add the middleware in your slim application.
Simple way
In this case, You must ensure this line is first added and on top of other middlewares
$app->add(new \Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware);
$app->add(new \Other\MiddlewareA);
$app->add(new \Other\MiddlewareB);
Better DI
In this case, You can place this line anywhere no position required
$app->add(new \Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware($app));
Global mode
In this case, The following code can make Whoops errors in the global scope, whether you have destroyed the program's life cycle
$whoopsGuard = new \Zeuxisoo\Whoops\Provider\Slim\WhoopsGuard();
$whoopsGuard->setApp($app);
$whoopsGuard->setRequest($container['request']);
$whoopsGuard->setHandlers([]);
$whoopsGuard->install();
Custom Whoops Handler
In this case, You can push custom handler to whoops. For example:
A handler like:
$simplyErrorHandler = function($exception, $inspector, $run) {
$message = $exception->getMessage();
$title = $inspector->getExceptionName();
echo "{$title} -> {$message}";
exit;
};
Middleware case like:
new \Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware($app, [$simplyErrorHandler]);
Global mode case like:
$whoopsGuard = new \Zeuxisoo\Whoops\Provider\Slim\WhoopsGuard();
$whoopsGuard->setHandlers([$simplyErrorHandler]);
E.g. Opening referenced files with your favorite editor or IDE and so on
$app = new App([
'settings' => [
// Enable whoops
'debug' => true,
// Support click to open editor
'whoops.editor' => 'sublime',
// Set page title
'whoops.page_title' => 'Custom page title',
// Display call stack in orignal slim error when debug is off
'displayErrorDetails' => true,
]
]);
Version 0.3.0
- The
whoops
library is installed by default base on the Whoops Framework Integration Document
Version 0.2.0
- You must to install the
whoops
library manually.
Run the test cases
php vendor/bin/phpunit