Home > PHP Framework > ThinkPHP > How to configure error page in thinkphp5.0

How to configure error page in thinkphp5.0

WBOY
Release: 2023-05-29 11:04:51
forward
1221 people have browsed it

I. The role of ThinkPHP 5.0 error page

  • The error page is mainly used to capture errors that occur when the application is running and Provides access to the error log.

  • The error page also supports real-time recording of error information, which can quickly troubleshoot and solve errors in the production environment.

II. Configuring the error page

Configuring the error page needs to be done in the application's configuration file, for example in config.php Add the following configuration to the file:

'exception_handle'        => 'app\index\exception\Http',
Copy after login

where app\index\exception\Http refers to the namespace and class name of the exception handling class. The exception handling class needs to inherit the think\exception\Handle class and override the render method to output custom exception information.

III. Default settings for error pages

The default error page in ThinkPHP 5.0 contains the following:

  1. Exception class Name

  2. Exception error code

  3. Exception error description

  4. Exception error file and line number

  5. Exception traceback information

The above information can help quickly locate the error location and perform tracking analysis. In addition, the error page also provides action buttons so that developers can perform some common operations.

IV. Custom error page

The error page also supports customization, just inherit the think\exception\Handle class in the controller , and rewrite the render method, for example:

namespace app\index\exception;

use think\exception\Handle;

class Http extends Handle
{
    public function render(\Exception $e)
    {
        if ($e instanceof HttpException) {
            $status = $e->getStatusCode();
        } else {
            $status = 404;
        }
        $data = [
            'status' => $status,
            'message' => $this->getMessage($e),
            'exception' => $this->isDebug() ? $this->getTrace($e) : [],
        ];
        return json($data);
    }
}
Copy after login

The above code shows how to customize exception information and return error information in JSON object format when an error occurs.

The above is the detailed content of How to configure error page in thinkphp5.0. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template