Integrating Whoops Error Handler into ThinkPHP6
This tutorial explains how to install the Whoops library via Composer and configure ThinkPHP6’s ExceptionHandle to replace the default error page with Whoops’ detailed, user‑friendly exception view, including code examples and a final verification.
During the Spring Festival the author became familiar with ThinkPHP6 and built a blog, but the default exception page was unhelpful.
To improve debugging, the Whoops library is introduced, which provides a pretty error page.
1. Install Whoops via Composer
<code>composer require filp/whoops</code>Note: any syntax errors in the project must be fixed before running the command.
2. Let Whoops handle ThinkPHP6 exceptions
Add the following code to /app/ExceptionHandle.php inside the render() method:
<code>// Add custom exception handling
if (ENV('APP_DEBUG')) {
// If the exception is HttpResponseException, return it unchanged
// JUMP plugin's success, error, and result methods all throw HttpResponseException
if ($e instanceof HttpResponseException) {
return $e->getResponse();
}
// Let Whoops handle the exception
$whoops = new \Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());
return Response::create(
$whoops->handleException($e),
'html',
500
);
}</code>After adding the code, an intentional undefined function is placed in the blog's index method to trigger an error.
The resulting page shows Whoops' detailed error screen, confirming the integration works.
3. Conclusion
The author revisited PHP7's exception mechanism while implementing Whoops and notes that try/catch should be used for business logic instead of if/else. A future article will discuss deeper insights into PHP7 exceptions.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.