During the Spring Festival, I got familiar with TP6 and wrote a blog program of TP6, but the abnormal page of the system was really another headache, and I could not find out which line of code caused the problem in many cases.

Therefore, he especially wanted to introduce whoops. After a series of studies, he finally found a solution:

1. Install whoops using Composer

Run the following command: composer require filp/whoops

Note: If there are syntax errors in the imported files, you need to handle the syntax errors in advance before installing composer. Otherwise, errors will continue to be reported.

2. Use whoops to take over exception handling of TP6

Add the following code to the render() method of the /app/ exceptionHandle.php file:

// Add custom exception handlingif (ENV('APP_DEBUG'// The success,error, and result methods of the JUMP plugin return HttpResponseExceptionif ($e instanceof HttpResponseException) {
        return $e->getResponse(); } // Whoops takeover request is abnormal$whoops = new \Whoops\Run;
    $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());
    return Response::create(
        $whoops->handleException($e),
        'html', 500); }Copy the code
The diagram below:


2. 3. Smart water bottle

I wrote a nonexistent function in the index method on the front page of my blog:

Refresh the page to see, good

OK. The familiar Whoops is back.

4. Afterword.

In order to use whoops in ThinkPHP6, I took a close look at the exception mechanism in PHP7 and finally introduced whoops.

While learning about exception handling, I also discovered that in PHP7 you should use try()catch() instead of if()else() for business processing.

I will write a separate post about my experience with PHP7 exceptions.

I also hope you can continue to follow my blog at http://laoliu.pro


Laoliu.pro/PHP /6.html