My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
EnvironmentClass  
Describes workings and usage of the Yii Environment-class.
Updated Mar 2, 2012 by marcovtw...@gmail.com

Environment-class

Original sources: http://www.yiiframework.com/doc/cookbook/73/

Simple class used to set configuration and debugging depending on environment. Using this you can predefine configurations for use in different environments, like development, testing, staging and production.

The main config (main.php) is extended to include the Yii paths and debug flags. There are mode_ENVIRONMENT.php files for overriding and extending main.php for specific environments. Additionally, you can overrride the resulting config by using a local.php config, to make changes that will only apply to your specific installation.

This class was designed to have minimal impact on the default Yii generated files. Minimal changes to the index/bootstrap and existing config files are needed. You can optionally override the environment by creating a mode.php in the config directory.

The Environment is determined with PHP's getenv(), which searches $_SERVER and $_ENV. There are multiple ways to set the environment depending on your preference. Setting the environment variable is trivial on both Windows and Linux, instructions included.

If you want to customize this class or its config and modes, extend it! (see ExampleEnvironment.php)

Installation

  1. Put the yii-environment directory in protected/extensions/
  2. Modify your index.php (and other bootstrap files)
  3. Modify your main.php config file and add mode specific configs
  4. Set your local environment

Setting environment

Here are some examples for setting your environment to DEVELOPMENT.

  • Windows:
    1. Go to: Control Panel > System > Advanced > Environment Variables
    2. Add new SYSTEM variable: name = YII_ENVIRONMENT, value = DEVELOPMENT
  • Linux:
    1. Modify your profile file:
      • Locally: ~/.profile or ~/.bash_profile (exact filename depends on your linux distro)
      • Globally: /etc/profile
      • Apache: /etc/apache2/envvars (if apache process doesn't use bash shell, try this file)
    2. Add: export YII_ENVIRONMENT="DEVELOPMENT"
  • Apache only: (cannot be used for console applications)
    1. Check if mod_env is enabled
    2. Modify your httpd.conf or create a .htaccess file
    3. Add: SetEnv YII_ENVIRONMENT DEVELOPMENT
  • Project only:
    1. Create a file mode.php in the config directory of your application.
    2. Set the contents of the file to: DEVELOPMENT

After setting environment/apache vars, you probably need to relog and restart the server.

Problems?

  • Q: After setting environment var, I get "Environment cannot be determined" when accessing the web application.
  • A: Make sure that where the Apache process starts, it can access the environment variable (by setting it as a system/global var).

Index.php usage example:

See yii-environment/example-index/ or use the following code block:

<?php
// set environment
require_once(dirname(__FILE__) . '/protected/extensions/yii-environment/Environment.php');
$env = new Environment();
//$env = new Environment('PRODUCTION'); //override mode

// set debug and trace level
defined('YII_DEBUG') or define('YII_DEBUG', $env->yiiDebug);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', $env->yiiTraceLevel);

// run Yii app
//$env->showDebug(); // show produced environment configuration
require_once($env->yiiPath);
$env->runYiiStatics(); // like Yii::setPathOfAlias()
Yii::createWebApplication($env->configWeb)->run();

Structure of config directory

Your protected/config/ directory will look like this:

  • config/main.php (Global configuration)
  • config/mode_development.php (Environment-specific configurations)
  • config/mode_test.php
  • config/mode_staging.php
  • config/mode_production.php
  • config/local.php (Optional, local override for mode-specific config. Don't put in your SVN!)

Modify your config/main.php

See yii-environment/example-config/ or use the following code block: Optional: in configConsole you can copy settings from configWeb by using value key inherit (see examples folder).

<?php
return array(
    // Set yiiPath (relative to Environment.php)
    'yiiPath' => dirname(__FILE__) . '/../../../yii/framework/yii.php',
    'yiicPath' => dirname(__FILE__) . '/../../../yii/framework/yiic.php',
    'yiitPath' => dirname(__FILE__) . '/../../../yii/framework/yiit.php',

    // Set YII_DEBUG and YII_TRACE_LEVEL flags
    'yiiDebug' => true,
    'yiiTraceLevel' => 0,

    // Static function Yii::setPathOfAlias()
    'yiiSetPathOfAlias' => array(
        // uncomment the following to define a path alias
        //'local' => 'path/to/local-folder'
    ),

    // This is the main Web application configuration. Any writable
    // CWebApplication properties can be configured here.
    'configWeb' => array(
        (...)
    ),

    // This is the Console application configuration. Any writable
    // CConsoleApplication properties can be configured here.
    // Leave array empty if not used.
    // Use value 'inherit' to copy from generated configWeb.
    'configConsole' => array(
        (...)
    ),
);

Create mode-specific config files

Create config/mode_<mode>.php files for the different modes These will override or merge attributes that exist in the main config. Optional: also create a config/local.php file for local overrides

<?php
return array(
    // Set yiiPath (relative to Environment.php)
    //'yiiPath' => dirname(__FILE__) . '/../../../yii/framework/yii.php',
    //'yiicPath' => dirname(__FILE__) . '/../../../yii/framework/yiic.php',
    //'yiitPath' => dirname(__FILE__) . '/../../../yii/framework/yiit.php',

    // Set YII_DEBUG and YII_TRACE_LEVEL flags
    'yiiDebug' => true,
    'yiiTraceLevel' => 0,

    // Static function Yii::setPathOfAlias()
    'yiiSetPathOfAlias' => array(
        // uncomment the following to define a path alias
        //'local' => 'path/to/local-folder'
    ),

    // This is the main Web application configuration. Any writable
    // CWebApplication properties can be configured here.
    'configWeb' => array(
        (...)
    ),

    // This is the Console application configuration. Any writable
    // CConsoleApplication properties can be configured here.
    // Leave array empty if not used
    // Use value 'inherit' to copy from generated configWeb
    'configConsole' => array(
        (...)
    ),
);
Comment by JEETU2...@gmail.com, Feb 13, 2012

i got some error while running this :"Environment mode cannot be determined, see class for instructions.' in C:\wamp\www\mytestsite\protected\extensions\yii-environment\Environment.php on line 284:

portalfever.com

Comment by project member marcovtw...@gmail.com, Mar 2, 2012

Try the steps mensioned under "Setting environment" on this page. Reboot your server/machine after completing the steps.


Sign in to add a comment
Powered by Google Project Hosting