Home  >  Article  >  php教程  >  How does Thinkphp implement exception error handling?

How does Thinkphp implement exception error handling?

WBOY
WBOYOriginal
2016-11-30 23:59:381597browse

Solution: Find the Thinkphp3.2.3 manual, the content is as follows!

Different from PHP's default exception handling, ThinkPHP throws not a simple error message, but a humanized error page, as shown in the figure below:

How does Thinkphp implement exception error handling?

Specific error information can only be displayed in debugging mode. If it is in deployment mode, you may see a simple prompt text, such as:

How does Thinkphp implement exception error handling?

Once the debugging mode is turned off, no specific error message will be prompted after an error occurs. If you still want to see the specific error message, you can set it as follows:

Set in ThinkPHP/Conf/convention.php

'SHOW_ERROR_MSG'        =>  true,    // 显示错误信息

If you try to access a module or operation that does not exist in deployment mode, a 404 error will be sent.

In debugging mode, an exception will be automatically thrown once a serious error occurs in the system. You can also use ThinkPHP's built-in E method to manually throw an exception.

E('新增失败');

Can also support exception codes (default is 0), for example:

E('信息录入错误',25);

You can also use the throw keyword to throw an exception. The following writing is equivalent:

throw new \Think\Exception('新增失败');

We can customize the display of the exception page. The system's built-in exception template is in Thinkphp/Tpl/think_exception.tpl in the system directory. You can modify the display of the exception page by modifying the system template.

Also modify the system default exception template file by setting the TMPL_EXCEPTION_FILE configuration parameter, for example:

Set in ThinkPHP/Conf/convention.php

'TMPL_EXCEPTION_FILE' => APP_PATH.'/Public/exception.tpl'

Exception variables that can be used in exception templates are:

$e['file']异常文件名
$e['line'] 异常发生的文件行数
$e['message'] 异常信息
$e['trace'] 异常的详细Trace信息

Because the exception template uses native PHP code, it can also support the use of any PHP method and system variables.

After an exception is thrown, a specific error message will usually be displayed. If you do not want the user to see the specific error message, you can turn off the display of the error message and set a unified error message, for example:

Set in ThinkPHP/Conf/convention.php

'SHOW_ERROR_MSG' =>    false,
'ERROR_MESSAGE'  =>    '发生错误!'

After setting, all exception pages will only display the prompt message "An error occurred!", but the specific error information can still be viewed in the log file.

By default of the system, the debugging mode turns on the error message display, and the deployment mode turns off the error message display.

Another way is to configure the ERROR_PAGE parameter to point all exceptions and errors to a unified page, thereby preventing users from seeing exception information. This is usually used in deployment mode. The ERROR_PAGE parameter must be a complete URL address, for example:

Set in ThinkPHP/Conf/convention.php

'ERROR_PAGE' =>'/Public/error.html'     系统默认为空

If it is not in the current domain name, you can also specify the domain name:

'ERROR_PAGE' =>'http://www.myDomain.com/Public/error.html'

Note that the page pointed to by ERROR_PAGE can no longer use abnormal template variables.

Statement:
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