首页 > 解决方案 > 如何判断 CakePHP 3.x 中的调试模式是否开启

问题描述

我想知道如何在 env() 函数中检索 var ...

/**
 * Debug Level:
 *
 * Production Mode:
 * false: No error messages, errors, or warnings shown.
 *
 * Development Mode:
 * true: Errors and warnings shown.
 */
'debug' => filter_var(env('DEBUG', true), FILTER_VALIDATE_BOOLEAN),

现在我正在使用

<?php if(DEBUG == true) { ?>

但这会引发错误

Use of undefined constant DEBUG - assumed 'DEBUG' (this will throw an Error in a future version of PHP)

标签: phpcakephpcakephp-3.0cakephp-3.6

解决方案


As suggested by ndm you can use read method to check whether debug mode is ON or OFF.

Add this in your controller

use Cake\Core\Configure;

and then use read method like this:

if (Configure::read('debug')) {
  echo "Debug mode is ON";
 } else {
  echo "Debug mode is OFF";
}

Cakephp -> Configuration -> Reading Configuration Data


推荐阅读