My development environment

After create many testdrive application with Yii, I found out a better way to set up a dev environment using Apache Virtual host feature, and Yii flexible configuration files.

In the index file, I use this setting:

if (! empty($_SERVER['HTTP_HOST'])){

	$hostname = $_SERVER['HTTP_HOST'];

} elseif (! empty($_SERVER['SERVER_ADDR'])) {

	$hostname = $_SERVER['SERVER_ADDR'];

} else {

	$hostname = 'main';


// change the following paths if necessary



Declare separate configuration files, equivalent to $hostname, to support multiple project based on the same code base.

protected/config/dev.localhost.php --> This file will be used when access URL dev.localhost/yiiproject

protected/config/test.localhost.php  --> This file will be used when access URL test.localhost/yiiproject

By adding virtual host and edit the /etc/hosts file, I can easily point those hostname into localhost.

This save alot of copy and paste between projects, because they all use the same code base.


But why not use setEnv to set variables in apache ?

This sounds even better to me :)


setEnv APPLICATION_ENV development


..... getenv('APPLICATION_ENV') .......



We use the APPLICATION_ENV solution, stuck with the Zend convention just in case. Instead of putting that code into the index file however we put it in the main config so it merges the environment config with the main config and that behaviour is encapsulated into the config area.

This technique is implemented in this extension: