Handling Yii Debug Mode

If you're working with Yii, you most probably know what YII_DEBUG constant is and you know that it should be disabled on production servers. Default Yii bootstrap just put a note in index.php saying that you should remove "define-yii-debug-true" line in production mode, but how would you handle that? I've seen few different approaches.

Create specific file for debug mode

It might be something like debugmodeon in your project directory. In your index.php you check if that file exists or not and enable/disable debug accordingly. This approch forces you to apply some VCS stuff, for example you have to put debugmodeon into .gitignore.

Put settings into environment config

If you use an architecture like YiiBoilerplate with different config for specific environment, you might put code to enable/disable debug in those config files, but... In this case you should include that config before Yii being included, otherwise Yii will define constant itself and you will get a notice that "Constant YII_DEBUG already defined". So you have to either replace CMap::mergeArray with something else, or load CMap class directly without Yii autoload.

Use server environment variable

In my opinion best approach is to use server environment variable (like APPLICATION_ENV/RAILS_ENV in ZF/RoR). It's quite simple and you don't have to handle .gitignore or do some magic with configs.

if (!defined('YII_DEBUG') && getenv('YII_DEBUG')) {
    define('YII_DEBUG', true);
// In apache:
// SetEnv YII_DEBUG 1

// Or nginx:
// fastcgi_param YII_DEBUG 1;

You can even set that value for whole server, and every project will use it. The only drawback is that you have to know apache or nginx config a little bit :)

comments powered by Disqus
Made with in SPb