Installed yii-environment
背景 Background
Yiiではdevelopment、production環境は用意されているが、
それ以上環境を追加するにはyii-environmentで追加するのが、
一般的なようなので導入してみます。
目標 Goal
- 環境依存は設定ファイルで保持できる事
- ソースコード上では環境を意識しなくて良い事
環境 Environment
- CentOS6.4
- PHP5.5.3 with phpenv and anyenv
- Yii1.1.14
- yii-environment3.2r82 (ブログ掲載時の最新)
手順 Operation
インストール作業
サンプルの環境別設定ファイルをprotected/configを全てコピーしていますが、
作る予定のない環境設定ファイルはコピー不要です。
設定ファイルmode_*.phpはmain.phpの設定よりも優先されます。
$ cd /usr/local/src $ sudo wget http://yii-environment.googlecode.com/files/yii-environment-3.2-r82.zip $ sudo chown -R `whoami` yii-environment-*.zip $ unzip yii-environment-*.zip -d $HOME/sample/protected/extensions $ cd $HOME/sample $ mv protected/extensions/yii-environment-* protected/extensions/yii-environment $ cp -p protected/extensions/yii-environment/example-config/mode_* protected/config $ vi protected/config/main.php 8 $YII_DIR_PATH='/usr/local/bin/yii/framework'; 9 return array( 10 // Set yiiPath (relative to Environment.php) 11 'yiiPath' => $YII_DIR_PATH.'/yii.php', 12 'yiicPath' => $YII_DIR_PATH.'/yiic.php', 13 'yiitPath' => $YII_DIR_PATH.'/yiit.php', 14 // Set YII_DEBUG and YII_TRACE_LEVEL flags 15 'yiiDebug' => false, 16 'yiiTraceLevel' => 0, 17 // Static function Yii::setPathOfAlias() 18 'yiiSetPathOfAlias' => array( 19 // uncomment the following to define a path alias 20 //'local' => 'path/to/local-folder' 21 ), 22 // This is the main Web application configuration. Any writable 23 // CWebApplication properties can be configured here. 24 'configWeb' => array( 25 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', .... 82 // application-level parameters that can be accessed 83 // using Yii::app()->params['paramName'] 84 'params'=>array( 85 // this is used in contact page 86 'adminEmail'=>'webmaster@example.com', 87 ), 88 ), 89 // This is the Console application configuration. Any writable 90 // CConsoleApplication properties can be configured here. 91 // Leave array empty if not used. 92 // Use value 'inherit' to copy from generated configWeb. 93 'configConsole' => array( 94 ), 95 ); $ vi index.php 1 <?php 2 // set environment 3 require_once(dirname(__FILE__).'/protected/extensions/yii-environment/Environment.php'); 4 $env = new Environment(); 5 // set debug and trace level 6 defined('YII_DEBUG') or define('YII_DEBUG', $env->yiiDebug); 7 defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', $env->yiiTraceLevel); 8 // run Yii app 9 require_once($env->yiiPath); 10 $env->runYiiStatics(); // like Yii::setPathOfAlias() 11 Yii::createWebApplication($env->configWeb)->run();
開発環境の設定
サーバの用途毎に適切に設定する必要があるため、ソース管理方法については検討が必要です。
サンプルコンフィグで準備されている環境一覧
- 開発環境(development)
- 本番環境(production)
- ステージング環境(staging)
- テスト環境(testing)
$ cd $HOME/sample $ echo 'development' > protected/config/mode.php $ vi protected/config/mode_development.php 12 return array( 13 // Set YII_DEBUG and YII_TRACE_LEVEL flags 14 'yiiDebug' => true, 15 'yiiTraceLevel' => 3, 16 // Static function Yii::setPathOfAlias() 17 'yiiSetPathOfAlias' => array( 18 // uncomment the following to define a path alias 19 //'local' => 'path/to/local-folder' 20 ), 21 // This is the specific Web application configuration for this mode. 22 // Supplied config elements will be merged into the main config array. 23 'configWeb' => array( 24 // Modules 25 'modules' => array( .... 64 ), 65 // This is the Console application configuration. Any writable 66 // CConsoleApplication properties can be configured here. 67 // Leave array empty if not used. 68 // Use value 'inherit' to copy from generated configWeb. 69 'configConsole' => array( 70 ), 71 );