Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2: How to prepare for debug and production environment?

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.

like image 728
robsch Avatar asked Mar 04 '15 10:03

robsch


2 Answers

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

like image 183
Chloe Avatar answered Nov 05 '22 23:11

Chloe


Yii2 (or at least the advanced application template) has a system of "environments". In this folder you can store the files that change per environment.

Those files are usually your bootstrap files (index.php) and "local" configuration files (things that override the main configuration).

The app template also has an "init" command that allows you to switch.

Basically what happens is that you add the entire environments-folder to your VCS, but you ignore the locations where those files are supposed to end up (like Ankit already stated). This way you can keep all different environment-dependant configurations in your VCS next to each other.

See here for a bit more information and here for an example of how this folder can look.

like image 6
Blizz Avatar answered Nov 06 '22 00:11

Blizz