Difference between #3 and #5 of
Use different environments (development, production, etc) in your app with EASY Environment Class

Changes

Title changed

Use different environments (development, production, etc) in your app with EASY Environment Class

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

Content changed

> Info:
 
> An extension was created based on this wiki page. See: <http://www.yiiframework.com/extension/yii-environment/>
 
 
 
Hi, I was having problems each time I had to test the site or edit something on my local machine, like changing the db connection, change the debug mode to false, the trace level to 0, disable Gii, etc. So I created a class to resolve all my problems, hope to help you in your projects!
[...]
============

Configuring the Easy Enviro
nment class ------------------------------------- 1. Create a new file in the config folder, name it 'environment.php'. 2. Paste the following code in your 'environment.php' file. ```php /** * This class helps you to config your Yii application * environment. * Any comments please post a message in the forum * Enjoy it! * * @name Environment * @author Fernando Torres | Marciano Studio * @version 1.0 */ class Environment {

const DEVELOPMENT = 100;
[...]
/**
* Initilizes the Enviro
nment class with the given mode
* @param constant $mode
*/
[...]
/**
* Sets the configuration for the choosen enviro
nment
* @param constant $mode
*/
[...]
/**
* Main configuration
* This is the general configuration that uses all enviro
nments
*/
private function _main() {
[...]
'params'=>array(
'adminEmail'=>'admin@example.com',
'enviro
nment'=> $this->_mode
)
);
[...]
);
}
}// END Enviro
nment Class ``` 3. Change the configuration for each environment:
* DEVELOPMENT
* TEST
[...]
```php
// Change the following paths if necessary
require_once(dirname(__FILE__).'/protected/config/enviro
nment.php'); $environment = new Environment(Environment::DEVELOPMENT) ; $yii = dirname(__FILE__).'/../../framework/yii.php'; defined('YII_DEBUG') or define('YII_DEBUG',$environment->getDebug()); defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', $environment->getTraceLevel()); require_once($yii); Yii::createWebApplication($environment->getConfig())->run(); ``` Changing your environment ------------------------ 5. To change the environment just you have to change the following code: ```php //For development $environment = new Environment(Environment::DEVELOPMENT); //For test $environment = new Environment(Environment::TEST); //For stage $environment = new Environment(Environment::STAGE); //For production $environment = new Environment(Environment::PRODUCTION); ``` Get your actual environment -------------------------- 6. You can check inside your application in what environment you are working with the following code: ```php // This will return the environment code number $currentEnvironment = Yii::app()->getParams()->environment; // Compare if the current environment is lower than production if($currentEnvironment < Environment::PRODUCTION){
//Your code here...
}else{
//Your code here...
}
[...]
7 0
7 followers
Viewed: 76 785 times
Version: 1.1
Category: Tutorials
Tags:
Written by: nancoder
Last updated by: marcovtwout
Created on: Jun 16, 2010
Last updated: 13 years ago
Update Article

Revisions

View all history