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!
Instructions
Create a new file in the config folder, name it 'environment.php'.
Paste the following code in your 'environment.php' file.
/** * 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; const TEST = 200; const STAGE = 300; const PRODUCTION = 400; private $_mode = 0; private $_debug; private $_trace_level; private $_config; /** * Returns the debug mode * @return Bool */ public function getDebug() { return $this->_debug; } /** * Returns the trace level for YII_TRACE_LEVEL * @return int */ public function getTraceLevel() { return $this->_trace_level; } /** * Returns the configuration array depending on the mode * you choose * @return array */ public function getConfig() { return $this->_config; } /** * Initilizes the Environment class with the given mode * @param constant $mode */ function __construct($mode) { $this->_mode = $mode; $this->setConfig(); } /** * Sets the configuration for the choosen environment * @param constant $mode */ private function setConfig() { switch($this->_mode) { case self::DEVELOPMENT: $this->_config = array_merge_recursive ($this->_main(), $this->_development()); $this->_debug = TRUE; $this->_trace_level = 3; break; case self::TEST: $this->_config = array_merge_recursive ($this->_main(), $this->_test()); $this->_debug = FALSE; $this->_trace_level = 0; break; case self::STAGE: $this->_config = array_merge_recursive ($this->_main(), $this->_stage()); $this->_debug = TRUE; $this->_trace_level = 0; break; case self::PRODUCTION: $this->_config = array_merge_recursive ($this->_main(), $this->_production()); $this->_debug = FALSE; $this->_trace_level = 0; break; default: $this->_config = $this->_main(); $this->_debug = TRUE; $this->_trace_level = 0; break; } } /** * Main configuration * This is the general configuration that uses all environments */ private function _main() { return array( // Base Path 'basePath' => dirname(__FILE__).DIRECTORY_SEPARATOR.'..', // Project Name 'name' => 'Project name', // Preloading 'log' component 'preload'=>array('log'), // autoloading model and component classes 'import' => array( 'application.models.*', 'application.components.*', ), // Application components 'components' => array( 'user' => array( // enable cookie-based authentication 'allowAutoLogin'=>true ), // Error Handler 'errorHandler'=>array( 'errorAction'=>'site/error', ), // URLs in path-format 'urlManager'=>array( 'urlFormat'=>'path', 'rules'=>array( '<controller:\w+>/<id:\d+>'=>'<controller>/view', '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', '<controller:\w+>/<action:\w+>'=>'<controller>/<action>' ), ), ), // Application-level parameters 'params'=>array( 'adminEmail'=>'admin@example.com', 'environment'=> $this->_mode ) ); } /** * Development configuration * Usage: * - Local website * - Local DB * - Show all details on each error. * - Gii module enabled */ private function _development () { return array( // Modules 'modules'=>array( 'gii'=>array( 'class'=>'system.gii.GiiModule', 'password'=>'your password', 'ipFilters'=>array(), 'newFileMode'=>0666, 'newDirMode'=>0777, ), ), // Application components 'components' => array( // Database 'db'=>array( 'connectionString' => 'Your connection string to your local development server', 'emulatePrepare' => false, 'username' => 'admin', 'password' => 'password', 'charset' => 'utf8', ), // Application Log 'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( // Save log messages on file array( 'class'=>'CFileLogRoute', 'levels'=>'error, warning,trace, info', ), // Show log messages on web pages array( 'class'=>'CWebLogRoute', 'levels'=>'error, warning, trace, info', ), ), ), ), ); } /** * Test configuration * Usage: * - Local website * - Local DB * - Standard production error pages (404,500, etc.) * @var array */ private function _test() { return array( // Application components 'components' => array( // Database 'db'=>array( 'connectionString' => 'Your connection string to your local testing server', 'emulatePrepare' => false, 'username' => 'admin', 'password' => 'password', 'charset' => 'utf8', ), // Fixture Manager for testing 'fixture'=>array( 'class'=>'system.test.CDbFixtureManager', ), // Application Log 'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( array( 'class'=>'CFileLogRoute', 'levels'=>'error, warning,trace, info', ), // Show log messages on web pages array( 'class'=>'CWebLogRoute', 'levels'=>'error, warning', ), ), ), ), ); } /** * Stage configuration * Usage: * - Online website * - Production DB * - All details on error */ private function _stage() { return array( // Application components 'components' => array( // Database 'db'=>array( 'connectionString' => 'Your connection string to your production server', 'emulatePrepare' => false, 'username' => 'admin', 'password' => 'password', 'charset' => 'utf8', ), // Application Log 'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( array( 'class'=>'CFileLogRoute', 'levels'=>'error, warning, trace, info', ), ), ), ), ); } /** * Production configuration * Usage: * - online website * - Production DB * - Standard production error pages (404,500, etc.) */ private function _production() { return array( // Application components 'components' => array( // Database 'db'=>array( 'connectionString' => 'Your connection string to your production server', 'emulatePrepare' => false, 'username' => 'admin', 'password' => 'password', 'charset' => 'utf8', ), // Application Log 'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( array( 'class'=>'CFileLogRoute', 'levels'=>'error, warning', ), // Send errors via email to the system admin array( 'class'=>'CEmailLogRoute', 'levels'=>'error, warning', 'emails'=>'admin@example.com', ), ), ), ), ); } }// END Environment Class
// Change the following paths if necessary require_once(dirname(__FILE__).'/protected/config/environment.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();
//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);
// 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... }
Enjoy it! ;D
Total 5 comments
To use my solution with testing, modify your protected/tests/bootstrap.php like this:
Merging config arrays is not working as I expected when both arrays have a certain value. Yii is using CMAP::mergeArray to do this properly. To fix this, you can use:
I will see about working out a solution where the config files don't have to be merged up front, to keep from including Yii files before the webapp is created.
Based on the versions from authors above, I created my own version of this script.
There is no environment specific code in the Environment.php class, everything is seperated into the config files. Impact on changing the index.php is minimal, but the configuration files need some slight modifications to incorporate the extra attributes. I might also turn them into classes at some point.
Environment is determined by Apache's SetEnv, but this can be easily changed at Environment::getMode()
Here is the code.
protected/config/Environment.php
index.php
protected/config/main.php
protected/config/mode_development.php:
protected/config/mode_test.php:
protected/config/mode_stage.php:
protected/config/mode_production.php:
It's a great class, but I prefer to see the passwords in separated files.
Usage
The usage is the same as nancoder class, in index.php
I really like this class. Great idea!
You could (if desirable) automate it a little further by letting the application set its own environment dynamically based on the application's path.
For instance, I have my application installed in a certain path like '/home/me/sites/my_application' on my development machine, and something like '/var/apps/live_application' on my production box, so you could use something like this in your index.php file:
$env = ( stristr( dirname(__FILE__), '/home/me' ) ? new Environment(Environment::DEVELOPMENT) : new Environment(Environment::PRODUCTION);Leave a comment
Please login to leave your comment.