为什么要关闭 trace 模式?
Trace 模式是 ThinkPHP 自带的调试模式,可以在页面底部方便地查看当前请求的相关信息,如请求参数、SQL 语句等,对问题定位非常有帮助。However, in a production environment, we do not want these sensitive information to be leaked and thereby affect the systems security.。此外,调试模式还会带来一定的性能损耗,因此我们有必要将其关闭。
如何关闭 trace 模式?
ThinkPHP 默认是开启 trace 模式的,我们可以通过设置 app_debug 参数来关闭 trace 模式。
在 config 目录下的 app.php 文件中,我们可以找到以下配置:
1
2
// 是否开启应用调试模式
app_debug => env(app_debug, true),
将 app_debug 的值设置为 false 即可关闭 trace 模式,代码如下所示:
1
2
// 是否开启应用调试模式
app_debug => false,
除了通过修改配置文件来关闭 trace 模式之外,我们还可以在应用程序的控制器(通常是基础控制器)中添加以下方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 构造函数
*
* 关闭调试模式
*/
public function __construct()
{
parent::__construct();
// 开发环境下,不关闭调试
if (config(app_debug)) {
return;
}
// 关闭调试
config(app_trace, false);
config(app_debug, false);
}
这个方法会在控制器初始化时被调用,如果 app_debug 配置为 false,则会关闭 trace 模式。
以上就是thinkphp如何关闭trace调试模式的详细内容,更多请关注php中文网其它相关文章!