Yii2: How to prepare for debug and production environment?

robsch picture robsch · Mar 4, 2015 · Viewed 28.8k times · Source

I know Yii defines and uses the constants YII_DEBUG and YII_ENV. Of course, they are set to to 'true' and 'dev' on my local machine. This is because the basic app template has prepared it this way in the index.php file. This file tells me also that I should remove those lines for production mode, i.e. on the production machine. Then those constants are set to 'false' and 'prod' by default. That's all okay and I understand it. (More information can be found on Defining Constants and Environment Constants.)

My question: How can I best handle these constants when index.php is contained in VCS? In one environment they should exist, in the other not. And it could be a test machine as well, of course. Which options do I have? I think this is also a matter of the deployment method. Currently, I'm just pushing via Git to the production machine, what is a primitive deployment IMO...

How do you do it? What do you suggest?

EDIT: Actually, handling the params files is the same issue.

Answer

Chloe picture Chloe · Mar 25, 2015

Here's my solution:

if ($_SERVER['SERVER_NAME'] == 'localhost' || $_SERVER['SERVER_NAME'] == '127.0.0.1') {
  defined('YII_DEBUG') or define('YII_DEBUG', true);
  defined('YII_ENV') or define('YII_ENV', 'dev');
}

Also for Heroku, Setup Yii2 Advanced on Heroku