Yii2 defining environment of console application

yii2 run console command from controller
yii2-console-runner
yii2 execute shell command
yii2 console log
yii2 cron job
yii application
yii2 tutorial
yii2 migration

How to define environment of console application in Yii2? In web application it's simple. You just have to add custom declaration into Apache vhost configuration and you are done. For example:

Virtual host configuration:

SetEnv APPLICATION_ENV production

Yii2 entry script:

$env = !empty( $_SERVER['APPLICATION_ENV'] ) ? $_SERVER['APPLICATION_ENV'] : 'dev';

defined( 'YII_DEBUG' ) or define( 'YII_DEBUG', true );
defined( 'YII_ENV' ) or define( 'YII_ENV', $env );

I have three environments: development, testing, production. So how should I set appropriate environment in console app? Because each environment has specific configuration, etc.

My application is based on basic app template.

If the environment should always be the same for all console apps, it's a good idea to set it in config/console.php. Just add something like

defined('YII_ENV') or define('YII_ENV', 'console');

When the environment should be different for different machines, gethostname and a switch-case would represent the cleanest way to set the environment.

switch (gethostname()) {
case "machine1": define('YII_ENV', "console-test"); break;
// more case statements
default: define('YII_ENV', "console");

Special Topics: Console Commands, Overview; Creating Commands; Console Command Action; Exit Codes To do so, define an environment variable named YII_CONSOLE_COMMANDS which  Environment class for Yii 2, used to set configuration for console and web apps depending on the server environment. Installation If you do not have Composer , you may install it by following the instructions at getcomposer.org .

I had the same issue a few days ago. The yii console app can not read environment variables from VirtualHost settings.

My workaround is to call php_uname() function to get the full operation system name that PHP is running on, and use that to differentiate my environment. If your three environment are running on three different (virtual) machines, you can try this way.

php_uname doc

Special Topics: Console Applications, Environment class for Yii 2, used to set configuration for console and web apps depending on to the require section of your application's composer.json file. The structure of console applications is very similar to a Yii web application. It consists of one or more yii\console\Controller classes, which are often referred to as commands in the console environment. Each controller can also have one or more actions, just like web controllers. Both project templates already have a console application

You can add lical file with environments, for example yii.env

<?php
defined('YII_ENV') or define('YII_ENV', 'prod');

and in file yii add:

if (is_file('yii.env')){
    include 'yii.env';
}

janisto/yii2-environment: Environment class for Yii 2, used , Yii Application Requirement Checker This script checks if your server configuration We'll start by creating our DEV environment folder from the command line:. YII_ENV: specifies which environment the application is running in. This will be described in more detail in the Configurations section. The default value of YII_ENV is 'prod', meaning the application is running in production environment. YII_ENABLE_ERROR_HANDLER: specifies whether to enable

Configuration - Mastering Yii, 34 * 35 * To run the console application, enter the following on the command line: and `param1`, `param2` refers to a set of named parameters that 43 * will be  Application extends from yii\base\Applicationby providing functionalities that are specific to console requests. In particular, it deals with console requests through a command-based approach: A console application consists of one or several possible user commands;

Yii 2: vendor/yiisoft/yii2/console/Application.php, Console Applications. Similarly, the following is the code for the entry script of a console application: #!/usr/bin/env php <?php /** * Yii console bootstrap file. Build Yii2 configuration arrays for web and console application; Initialize Yii environment (YII_DEBUG, YII_ENV) from environment variables; Load environment variables from a .env file; Get config options from environment variables; Load local configuration overrides; Streamline config initialization and Yii 2 bootstrapping; Installation

Entry Scripts, Build configuration arrays from config files and env vars. Build Yii2 configuration arrays for web and console application; Initialize Yii environment  Setting Up Your Remote Hosting Environment. The basic Yii application should run fine out of the box with Apache or an Nginx, I'll provide instructions for Apache and you can find instructions for Nginx here. Create a Repository at GitHub. Once I set up a Yii application template, I create a repository at Github:

Comments
  • You could simply modify console entry script : yiiframework.com/doc-2.0/…
  • Yes, I know I can :) But still I need some kind of identifier that defines current environment. In web application I can set such identifier in virtual hosts configuration.
  • Well, this has nothing to do with yii2, there are plenty of ways to do this.