Yii2: How to prepare for debug and production environment? Yii2: How to prepare for debug and production environment? php php

Yii2: How to prepare for debug and production environment?


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


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.


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:

<?phpdefined('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.