Skip to content

Instantly share code, notes, and snippets.

@yidas
Created January 3, 2019 03:50
Show Gist options
  • Save yidas/82511f140b105e9f129c281846fb725d to your computer and use it in GitHub Desktop.
Save yidas/82511f140b105e9f129c281846fb725d to your computer and use it in GitHub Desktop.
Yii2 App Basic Environment Setting Switch for WEB & Console

Yii2 App Basic Environment Setting Switch for WEB & Console

Server Variable Solution

Add server variable condition for ./web/index.php & ./yii:

if (isset($_SERVER['APP_ENV']) && $_SERVER['APP_ENV']=='production') {
    
    // Default is PROD 

} else {
    // DEV
    defined('YII_DEBUG') or define('YII_DEBUG', true);
    defined('YII_ENV') or define('YII_ENV', 'dev');
}

After that, you can switch environment easiliy.

Web

Nginx for example, set server variable into the site:

location ~ \.php$ {                                
                                                   
    # ENV Setting                                     
    fastcgi_param APP_ENV 'production';        

    include snippets/fastcgi-php.conf;                         
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}                                                  

Console

Add server variable setting before yii console:

APP_ENV="production" ./yii controller/action
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment