Running multiple sites from a common web root

I am writing code that will replace a legacy CF system. At some point in the past this system was forked to run a 2nd company, and the web root was copied to another folder. Of course, over the years the 2 systems diverged, fixes were not always put into both, and gradually the single code base become twice as hard to handle. One of my goals for this new system was to write it such that many sites could share the same code base.

To handle this, I decided to use Coldfusion's excellent ability to extend other CFC's. My directory structure looks something like this:

/mappingRoot
  /sharedCode
    /services
    /model
    /controllers
  /company1
    /services
    /model
    /controllers
  /company2
    /services
    /model
    /controllers

The majority of my code resides inside the sharedCode folder. The 'model' folder contains all of my transfer decorators. A problem I ran into was, Transfer.xml cannot accept any variables like a Coldspring file can. I was browsing through a the google transfer-dev list, and came across something Elliot posted about re-writing the transfer file when your application starts, and doing a string replace on it so you dont have to maintain multiple Transfer files.


<!---// Hack on app start to replace ${companyName} in transfer.xml and copy to company-specific locale for app ini //--->
      <cffile action="read" file="#expandPath('/mappingRoot/shared/transfer/Transfer.xml')#" variable="xferConfig" />
      <cffile action="write" file="#expandPath('/mappingRoot/' &amp;amp; this.name &amp;amp; '/transfer/Transfer.xml')#" nameconflict="overwrite" output="#replace(xferConfig, '${companyName}', this.name, 'ALL')#" />

This allows me to use my application name to drive where I am telling Transfer my decorators are located. Each decorator is layered like this:


/mappingRoot/companyName/model/object
/mappingRoot/sharedCode/model/object
/mappingRoot/sharedCode/model/baseTransferDecorator
transferDecoratorClass

This allows me to do several things. I can override my shared code decorator functions on a per-company basis if necessary (to facilitate custom code for only 1 company). I can place things in my baseTransferDecorator that all transfer objects across the entire system need (logging functions, custom save() function, etc).

I use this same method on my controllers and services as well, with a baseController being extended by a sharedCode controller, which is extended by the per-company controller. Right now the per-company customizations are empty, but it will be very simple down the road when companyA needs a tweak that companyB does not want.

I find its better to plan things like this early on rather than get everything coded and hope that you can do emergency patches!

If thinking of running multiples sites, go for hostgator or midphase. Another good cheap web hosting option is that of powweb.

0 comments

0 responses to “Running multiple sites from a common web root”

Leave a comment