19

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.

robsch
  • 9,358
  • 9
  • 63
  • 104

6 Answers6

7

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

Community
  • 1
  • 1
Chloe
  • 25,162
  • 40
  • 190
  • 357
6

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.

Blizz
  • 8,082
  • 2
  • 33
  • 53
  • Thanks, I have to think about it. Actually it is just part of the advanced application template, not Yii itself. But one should have the same environment issues with the basic template or in general with each type of application. – robsch Mar 04 '15 at 12:49
  • You are completely right. Imo the basic template should also feature the environments. Perhaps they should add a 3rd app template ("minimalistic" or so) that allows you to quickly test something. Then "basic" could become the "lightest configuration for professional use" or something – Blizz Mar 04 '15 at 13:00
  • [This page](https://www.my-yii.com/learn/view-episode/how-to-create-and-use-environments-in-yii-2-basic-application-template) explains the concept very well. – robsch Sep 05 '16 at 07:54
6

Another simple solution:

File index.php (goes into VCS repo):

<?php

@include 'my-env.php';

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/../config/web.php');

(new yii\web\Application($config))->run();

File my-env.php:

<?php

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

my-env.php won't be added to the VCS. It can exist or not. If not, the app runs automatically in production mode. my-env.php could be also placed into the config folder maybe.

This is a small improvement compared to Ankit's approach, so that the index.php can be added to the VCS. Hence, the VCS repo contains all required files and can be deployed without any manual modifictations.

robsch
  • 9,358
  • 9
  • 63
  • 104
5

Do You have access to config file where you set virtualhosts? If so You can just addsetEnv YII_DEBUG "true" between </Directory> and </VirtualHost>

2

Commit index.php once and then add it to .gitignore. So that you do not have to change it every time.

Simply add /web/index.php in .gitignore

ankitr
  • 5,992
  • 7
  • 47
  • 66
  • What if another developer checks out the project? You have to email them the index.php? – Chloe Mar 24 '15 at 23:37
  • index.php will be in remote repo and will be available to team members as part of clone, but it just won't track any local changes to index.php. It is the suggested approach of Yii2 as well. – SenG Jun 13 '15 at 16:44
  • init script would generate all (target) environment specific files when we run it including index.php. – SenG Oct 02 '17 at 15:14
0

Yii2 works with composer. One question is how to handle composer.lock.

In my Yii2 production sites I put composer.lock to .gitignore so I can decide on composer updates depending on production issues and keep a production version of composer.lock untouched during a git pull for updating production sites.

My last lines of .gitignore are:

 # exclude composer.lock from versioning 
 composer.lock
WeSee
  • 3,158
  • 2
  • 30
  • 58