search
HomePHP FrameworkThinkPHPFinally successfully used whoops to take over the exception handling of tp6!

The following tutorial column from thinkphp will introduce to you how to use whoops to take over the exception handling of tp6. I hope it will be helpful to friends in need!

Say goodbye to the abnormal pages of ThinkPHP6, let us embrace whoops!

I got familiar with TP6 during the Spring Festival, and also wrote a TP6 blog program, but the system's exception page is really a headache, and many times it cannot be viewed. Which line of code is the problem?

So I really wanted to introduce whoops. After a series of research, I finally found the solution:

1. Install whoops through composer

Run the command: composer require filp/whoops

Note: If the file imported by composer has syntax errors, you need to deal with the syntax errors in advance before installation, otherwise an error will always be reported.

2. Use whoops to take over the exception handling of tp6

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

// 添加自定义异常处理机制
if (ENV('APP_DEBUG')) {
    // 如果是HttpResponseException异常则原样输出
    // JUMP插件里的success,error和result方法均返回的是HttpResponseException异常
    if ($e instanceof HttpResponseException) {
        return $e->getResponse();
    }

    // Whoops 接管请求异常
    $whoops = new \Whoops\Run;
    $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());
    return Response::create(
        $whoops->handleException($e),
        'html',
        500
    );
}

As shown below:

Finally successfully used whoops to take over the exception handling of tp6!

3. Take a look at the result

I wrote an Non-existent function:

Finally successfully used whoops to take over the exception handling of tp6!

Refresh the page and see, OK

Finally successfully used whoops to take over the exception handling of tp6!

OK. The familiar whoops is back .

4. Postscript

In order to use whoops in thinkphp6, I carefully looked at the exception mechanism in PHP7 and finally introduced whoops. .

In the process of learning exception handling, I also found that try()catch() should be used for business processing in PHP7 instead of if()else().

I will write a separate article later to tell about my experience in studying the exceptions of PHP7.

I also hope that everyone can continue to pay attention to my blog http://laoliu.pro

Original link: http://laoliu.pro/php/6.html

The above is the detailed content of Finally successfully used whoops to take over the exception handling of tp6!. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment