Home > php教程 > php手册 > body text

How does Thinkphp implement exception error handling?

WBOY
Release: 2016-11-30 23:59:38
Original
1720 people have browsed it

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

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

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.

<code class="hljs bash">E(<span class="hljs-string">'新增失败');</span></code>
Copy after login

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

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

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

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

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

<code class="hljs php"><span class="hljs-string">'TMPL_EXCEPTION_FILE' => APP_PATH.<span class="hljs-string">'/Public/exception.tpl'</span></span></code>
Copy after login

Exception variables that can be used in exception templates are:

<code class="hljs bash"><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></code>
Copy after login

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

<code class="hljs 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></code>
Copy after login

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

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

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

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

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

source:php.cn
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!