search
Homephp教程php手册How does Thinkphp implement exception error handling?

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

<span class="hljs-string">'SHOW_ERROR_MSG'        =>  <span class="hljs-keyword">true,    <span class="hljs-comment">// 显示错误信息</span></span></span>

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(<span class="hljs-string">'新增失败');</span>

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

E(<span class="hljs-string">'信息录入错误',<span class="hljs-number">25);</span></span>

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

<span class="hljs-keyword">throw <span class="hljs-keyword">new \Think\<span class="hljs-keyword">Exception(<span class="hljs-string">'新增失败');</span></span></span></span>

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

<span class="hljs-string">'TMPL_EXCEPTION_FILE' => APP_PATH.<span class="hljs-string">'/Public/exception.tpl'</span></span>

Exception variables that can be used in exception templates are:

<span class="hljs-variable">$e[<span class="hljs-string">'file']异常文件名
<span class="hljs-variable">$e[<span class="hljs-string">'line'] 异常发生的文件行数
<span class="hljs-variable">$e[<span class="hljs-string">'message'] 异常信息
<span class="hljs-variable">$e[<span class="hljs-string">'trace'] 异常的详细Trace信息</span></span></span></span></span></span></span></span>

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

<span class="hljs-string">'SHOW_ERROR_MSG' =>    <span class="hljs-keyword">false,
<span class="hljs-string">'ERROR_MESSAGE'  =>    <span class="hljs-string">'发生错误!'</span></span></span></span>

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

<span class="hljs-string">'ERROR_PAGE' =><span class="hljs-string">'/Public/error.html'     系统默认为空</span></span>

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

<span class="hljs-symbol">'ERROR_PAGE' =><span class="hljs-symbol">'http:<span class="hljs-comment">//www.myDomain.com/Public/error.html'</span></span></span>

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

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor