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

 
public function beforeFilter() {
$this-> fetchSettings();
 
//Function which reads settings from country table and populate them in Configure
function fetchSettings(){
$domain = preg_replace('/^www\./', '', $_SERVER['SERVER_NAME']);
//Loading model on the fly
$this->loadModel('Domain');
//Fetching just current domain records
$country_settings = $this->Domain->find('all', array('conditions' =>
array('Domain.domain' => $domain)));
foreach($domain_settings as $value){
//create vars to use in Configure::Write
Configure::write('domain', $value['Domain']['domain']);
Configure::write('site_name', $value['Domain']['site_name']);
Configure::write('stop_track', $value['Domain']['stop_track']);
Configure::write('ga_code', $value['Domain']['ga_code']);
Configure::write('contact_email', $value['Domain']['contact_email']);
Configure::write('meta_title', $value['Domain']['meta_title']);
Configure::write('meta_keywords', $value['Domain']['meta_keywords']);
Configure::write('meta_keywords_cards', $value['Domain']['meta_keywords_cards']);
Configure::write('meta_description', $value['Domain']['meta_description']);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Post Navigation