A while ago one of my favorite clients wanted an application that would basically set her up as an ASP. My client planned to charge a yearly fee for access to a customized version of this application. While each instance of the application would have its own database and style sheet, the application logic would be the same for all instances.

As it was, each instance would be accessed under its own subdomain. Therefore, I could have created a separate cakePHP installation for each subdomain, but I decided against that approach because bug fixes or enhancements would then need to be applied to each instance, one-by-one. I decided instead to share the cake and app folder, so application updates would only have to be applied once.

The filesystem

The file system on the web host was set up like this:

main_folder
  --public_html
  --subdomain1_folder
  --subdomain2_folder

Each instance got its own copy of the webroot folder in the subdomain's folder. That way I could customize the contents of the css, img, and js folders, thereby changing the style of each instance. The resulting file system looked like this:

main_folder
  --app
  --cake
  --public_html
  --subdomain1_folder
     --css
     --img
     --js
     --index.php

The copy of index.php that is in each subdomain folder had to be modified so that ROOT and APP_DIR are correct. Also, I added a new constant called CLIENTDB that is the name of the database config in the DATABASE_CONFIG object--more on that below.

The databases

With some help at the cakePHP Google Group I simply added each new database to config/database.php in the standard manner. With that set up, at runtime, the application knows which database to use because of the line

var $useDbConfig  =  CLIENTDB; 
which I added to app_model.php. If you remember from above, CLIENTDB was defined in index.php, the entry point for the application, which is unique for each subdomain. I just had to make sure what I defined CLIENTDB to be matched the variable I created in config/database.php.

Auto-generation

If, to you, making those modification by hand for each new subdomain seems like a pain in the knuckles, you and I think alike. To make setup of a new domain easier, I created a database to that stored the database and other configuration information for each instance of the application. I called it the manager database.

With the manager database I was able to modify config/database.php. Instead of a static list of databases, I made a call to the manager database and pulled in each subdomain's database configuration dynamically. I've posted an example of the new database.php.

The next step was to create a manager application that created the instance database, copied a standard webroot folder into the subdomain's folder and created the customized index.php file with the new CLIENTDB. Then I even created a script to automatically set up the subdomin.

In the end, I had a manager that generated an entire working instance of the application from a simple form that my client could use.