使 Zend\Debug\Debug::dump() 仅在开发模式下转储?

Make Zend\Debug\Debug::dump() only dump in development mode?

为了快速调试,我更喜欢 Zend\Debug\Debug::dump()。问题:有时我忘记删除旧的转储语句,它们将其投入生产。

如果 Debug::dump() 只打印一些东西就好了,当我在 development mode.

有没有无需将 Zend\Debug\Debug 转换为服务即可实现此目的的优雅方法?我喜欢简单明了的静态方法调用。可能是通过在启用开发模式时设置环境变量?

在您的 /public/.htaccess 中添加此代码。 SetEnv APPLICATION_ENV "development"

在您的 /public/index.php 文件中

/**
 * Set global ENV. Used for debugging
 */
if (isset($_SERVER['APPLICATION_ENV']) && $_SERVER["APPLICATION_ENV"] === 'development') {
    define("APP_ENV", 'development');
} else {
    define("APP_ENV", "production");
}

/**
 * Set default php.ini settings.
 *
 * Below lines includes security|error fixes
 */

/**
 * Handle reporting level
 */
error_reporting((APP_ENV === 'development' ? E_ALL : 0));

/**
 * Log errors into a file
 */
ini_set("log_errors", (APP_ENV === 'development'));

/**
 * Display of all other errors
 */
ini_set("display_errors", APP_ENV === 'development');

/**
 * Display of all startup errors
 */
ini_set("display_startup_errors", APP_ENV === 'development');

/**
 * Catch an error message emitted from PHP
 */
ini_set("track_errors", APP_ENV === 'development');

/**
 * Avoid serving non .php files as .php files
 */
ini_set('cgi.fix_pathinfo', 0);

/**
 * Helps mitigate xss
 */
ini_set('session.cookie_httponly', 1);

/**
 * Prevents session fixation
 */
ini_set('session.use_only_cookies', 1);

/**
 * Fixes files and server encoding
 */
mb_internal_encoding('UTF-8');

/**
 * Some server configurations are missing a date timezone
 */
if (ini_get('date.timezone') == '') {
    date_default_timezone_set('UTC');
}

当您的站点进入 public 时,只需将 .htaccess 环境变量更改为生产环境变量,所有调试选项都会消失。这就是我禁用此类调试选项和模块的方式,它工作正常。