Codeigniter Multiple Sites with HMVC and Packages

Building multiple websites with a single Codeigniter base using HMVC and Packages that share the same custom libraries across all sites.

I thought I'd document this as I haven't been able to find this solution in any one single blog.

I've a number of clients that have similar website functions but different layouts but maintaining modules/classes/libraries etc with bug-fixes and new functions, across all these sites is a nightmare. I could use some form of version control but it still means going into each site to update them. What I wanted was a single point for all my custom classes. libraries, js scripts and layout css files.

So this is my solution:

PHP 5.2

Codeigniter 2.02

Wiredesignz's Modular Extensions - HMVC (as of 11th June 2011).  Download here and the install instructions here.

Here is the file structure (with notes):

wwwroot/

system/  (standard system folder with no changes - Golden Rule "NEVER HACK CORE" )

              custom/  (modules, libraries and classes that will be shared across all websites)

libraries/  (shared libraries and third party stuff like tinymce)

modules/ (HMVC modules shared across all websites, like login, register etc)

models/ (models that don't belong to HMVC)

views/ (views that don't belong to modules)

js/

css/

website01/

.htaccess (see here for details on this file)

index.php (main index)

application/  (standard application folder)

/cache

/config

/controllers

/core

/errors

/helpers

/hooks

/language

/libraries

/logs

/models

/modules

/third_party

/views

assets/ (Just a place to store psd and things)

uploads/ (clients' uploads)

js/ (site specific js)

css/

images/

 

With the file structure out of the way, which of course you can have your own setup, lets begin.

We're going to need some constants across all sites so in the webroot/custom directory create a file called custom_config.php. add the following code.

<?php
define('CUSTOM_PATH', dirname(__FILE__));

 

At the top of the webroot/website01/index.php (replace website01 for your website name)

Just below the opening php tag

<?php
include('../custom/custom_config.php');

 

You can test this by adding

<?php
include('../custom/custom_config.php');
print CUSTOM_PATH;

Don't forget to remove it !!!

You will also need to change the path of the system folder inside this index.php file

$system_path = 'system';

to

$system_path = '../system';

 

Assuming you followed Wiredesignz's installation guide you should now have your welcome controller welcome.php inside /webroot/website01/application/modules/welcome/controllers/

Make these changes

class Welcome extends CI_Controller {

to

class Welcome extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->add_package_path(CUSTOM_PATH . '/');
    }

 

OK lets make a separate module that we can share

in the webroot/custom/modules/ create a new "Triad"

I'm just going to call mine dog for fun

webroot/custom/modules/dog/

controllers/

models/

views/

create a file called dog.php in webroot/custom/modules/dog/controllers/

ad add the following code.

<?php

class Dog extends MX_Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        print 'Hello I am a dog';
    }

}

 

OK if you navigate to the url /dog you well get an error.  HMVC doesn't know about the custom folder so we need to add this information in

webroot/website01/application/config/config.php

at the top of the file add this code

<?php

config['modules_locations'] = array(   APPPATH.'modules/' => '../modules/',
  CUSTOM_PATH .'/modules/'  => '../../../custom/modules/'
);

And providing you have the same directory setup as me then go back to your browser and voila you should now see the contents of the dog controller.

A Multiple website, single core Codeigniter with packages that allow you to share apps across all sites.

Happy programming

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.