Welcome Guest, Not a member yet? Register   Sign In
Multiple Sites with Modular Extensions - HMVC and Packages
#1

[eluser]Kevin Phillips[/eluser]
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 Modular Extensions - HMVC (as of 11th June 2011). You can download and view Wiredesign'z wiki from Wiredesignz

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/
website02/
website03/

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.

Code:
<?php
define(', dirname(__FILE__));

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

Just below the opening php tag

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

You can test this by adding

Code:
<?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

Code:
$system_path = ';

to

$system_path = '../system';

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

Make these changes
Code:
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.
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

Code:
<?php

config['modules] = 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.

Any improvements to this are welcome.

Happy programming

I've got a number of codeigniter and Drupal solutions on my personal blog kevinphillips.co.nz that I update regularly, if only for my own reference and sanity.
#2

[eluser]JulianM[/eluser]
Thanks for this. I usually came to the same troubles managing multiple projects and keeping everything up to date.
#3

[eluser]Kevin Phillips[/eluser]
I'm just going through Wiredesignz MX_ACL which also works, I'm building a patch that allows users to have multiple roles.

I'll post it here when I'm done.

Also note that codeigniter datamapper orm works with this setup. :-)
#4

[eluser]Unknown[/eluser]
Helpful post, thanks! Wanted to point out a couple of miskeys:

Code:
<?php
define(', dirname(__FILE__));

should be

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

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

should be

<?php
config['modules_locations'] = array(
  APPPATH.'modules/' => '../modules/',
  CUSTOM_PATH .'/modules/'  => '../../../custom/modules/'
);
#5

[eluser]Azraf[/eluser]
Great post. Thanks.
#6

[eluser]Unknown[/eluser]
Thank you Kevin Phillips for your post.
And dishwalla for your small fixes.

I followed your structure exactly, and got it working perfectly with four sub websites running off the some core.
I can run models from the custom modules as partials in the sub websites which is great as I want to be able to run the websites more as a platform. I can also overwrite module controllers in the core but creating module controllers with the exact same name in one of the sub websites.

I have a development version running on my local machine and have set up four virtual host as follows:
dev.website01.local, dev.website02.local, dev.website03.local, and dev.website04.local.
Again, this is working perfectly as expected.

My one question is as follows:
You have a folder for JS and CSS in the custom folder, which like the modules, I would assume could be accessed by all four sub websites. For example, at the moment I have a default header and footer in the custom folder which all four websites inherit. But using the base_url() function, they only have access to dev.website01.local and so on. Not the custom folder. I have the constant CUSTOM_PATH working, but you can't reference JS and CSS files with that. The CSS file I can live without because in theory each sub website would have it's own CSS. But the JS will always be the same across all sites.

Would I be better to create another virtual host for the core files i.e. dev.corewebsite.local and just use that to reference the JS and CSS files?




Theme © iAndrew 2016 - Forum software by © MyBB