How to serve multiple domain name websites with one CakePHP application

Say you have a series of websites that are the same with the exception of city, topic, etc. It could be a real estate listing of different countries and you want a separate website for each or maybe different sites for different brands, product categories, client, etc. You could create subdomains or parameterized filtering in a single website on single domain, but what if you wanted to keep sites separate?

You could just use one CakePHP application and one MySQL datababse to serve websites for multiple domains.

All domains have to be hosted on shared hosting. Shared hosting has to allow pointing multiple domains to one subdomain directory. I’ve done it with Webfaction and Justhost shared web hosting.

Put the CakePHP application files into the shared subdomain directory connect that application to the single MySQL database, which should have all the data for every website.

Create your CakePHP application as normal but add the following things so that you can filter database records specific to each website/domain being served/requested:

  • A table called domain (or whatever you want with as many columns as you need) that will hold domain specific values that are retrieved to be used in in application. But at a minimum it should have the each site’s domain so you can do a lookup to match it to the domain being requested.
  • Add domain table foreign key (domain_id) to other tables so that they can be filtered by domain when retrieving data. (This means likely you will require additional steps to ensure that the foreign key is written to your tables. How you do that is up to you but for example if you allow users to create accounts or post comments then you can simply record the domain_id (for example you CakePHP Configure::read value when new user or comment is added.)
  • Add a getDomainSettings function to AppController and call it in AppController beforeFilter. This getDomainSettings function will read domain name from current site visitor and use it as parameter to filter database records to just that domain’s values. You can get these into application many different ways but it is nice and easy to use CakePHP Configure::write variables that can be used elsewhere in application.
  • As required, add conditions when retrieving model data using CakePHP Configure:write in other controllers’ to retrieve only records for the current domain and modify view presentation.

The alternative to this is maintaining 6 separate CakePHP applications and MySQL databases. So this is a nice clean and easy solution.

Lots of potential here, you could also swap out css styles or CakePHP themes based on domain and make sites totally different.

Code is on Github:

https://github.com/sitrucp/single_cakephp_mysql_multiple_domain_app

Introducing Speedvisit – a quick and easy way to record visits from people

Here is a demo web application / SaaS called SpeedVisit that individuals and companies can use to easily and quickly record visits from people to record basic customer demographic information.

Features include:

  • Groups – create a group so you add other users, locations and referrers to it.
  • Users – add additional users to your group as you want.
  • Locations – add locations where visits will take place.
  • Referrers – add referrers to record how visitor was referred.

The definition of ‘visit’ is pretty broad. It could be customers, co-workers, classmates, attendees in many situations including:

  • Retail Stores
  • Staff Meetings
  • Convenience Stores
  • Restaurants
  • Street Vendors
  • Art Studios
  • Non-Profits
  • Community Clubs
  • Sports Events
  • Social Events
  • Trade Shows

The demographic information that you can record currently includes:

  • Gender
  • Age
  • Zipcode or postal code
  • Income
  • Prev Visit
  • Referrer
  • Location
  • Purchase
  • Email
  • Comments

The site is mobile friendly responsive design so it works on phones, tablets, laptops and desktops.

The data is totally portable with easy export of all visits to csv file. It is setup with SSL so your data will be securely transferred between your browser and the server.

This is a screenshot of the top of the visit capture page which is the central feature of the application and workflow.  Simply use radio buttons and choosers to select categorize the visitor and add record. The page will then be ready for the next visitor.

speedvisit1

 

The data is available for anyone else to view it, to view aggregated statistics, etc. This is screenshot of the recorded visit data.

speedvisit2

You can sign into the demo account to see what sample account and visits look like too.

Single CakePHP application & MySQL database to serve multiple domains

I have a bunch of websites that have the same structure and content that vary only by the category for each site. So content for all sites can reside in a single database schema and filtered to specific categories for each site.

In order to do this I created a custom CakePHP application with one MySQL database to serve multiple public facing websites each with their own unique domain.

In order to make this work you need a hosting account that will allow you to map website serving directories to domain names. Different hosting providers may have different methods of doing this.

The secret to make this work is to have the application parse the domain name that is calling the application and then use that to filter the database records for that domain category.

In CakePHP do this by creating a function in the AppController that will check to see what domain the visitor is browsing to and then to retrieve that domain’s specific values from the special table to filter the CakePHP application. You will need to to call this function in the AppController beforeFilter function. I have included an example function named ‘fetchSettings’ below.

It will also be very helpful to make a table that contains meta data for each domain such as keywords, analytics tracking code, etc. I didn’t do include a different CSS file for each domain but you could include CSS file variable to refer to separate CSS file for each domain.

 

Check Github repository for more information:

https://github.com/sitrucp/single_cakephp_mysql_multiple_domain_app

<?php

App::uses('Controller', 'Controller');

class AppController extends Controller {
    
    public function beforeFilter() {
        $this-> getDomainSettings(); 	
    }
    
    //function which reads settings from domain table  
    function getDomainSettings(){
        //get current domain from server name as variable
        $domain = preg_replace('/^www\./', '', $_SERVER['SERVER_NAME']);
        //Load domain model data
        $this->loadModel('Domain');
        //retrieve only current domain table record
        $domain_settings = $this->Domain->find('all', array('conditions' => 
        array('Domain.domain' => $domain)));  
        foreach($domain_settings as $value){
            //create Configure::Write variables from domain record to use elsewhere in application
            Configure::write('domain', $value['Domain']['domain']);
            Configure::write('domain_id', $value['Domain']['id']);
            Configure::write('ga_code', $value['Domain']['ga_code']);
            Configure::write('meta_title', $value['Domain']['meta_title']);
            Configure::write('meta_keywords', $value['Domain']['meta_keywords']);
            Configure::write('meta_description', $value['Domain']['meta_description']); 
            
            //etc retrieve as many domain specific values as required from database
        }
    }
}

?>