How to share the controller and model between two applications in CI?

advertisements

I am using CodeIgniter 2.1.1 and building a website using two environments- Production and Testing. Essentially, in the beginning, both the environments have the same program logic. But now there are several modifications to be made.

The idea is to share the controllers and models of production environment and test environment but have different views for both environment.

I have set up WAMP and in the WWW directory there are two separate folders: one for testing, another one for production. There are controllers , models ,views etc under each folder. CI is pointed appropriately and the base URLs are set.

My question is even, when I type the URL in the web browser to invoke controller of test environment, it should redirect and invoke the controller of production environment. Is that possible ?


Don't have it redirect. Have it include the file instead.

Assuming you have this file structure:

www-root
 |-production
 |   |-application
 |   |   |-controllers
 |   |   |-etc.
 |   |-etc.
 |
 |-test
 |   |-application
 |   |   |-controllers
 |   |   |-etc.
 |   |-etc.
 |

Create files with exactly the same name in both environments. We'll say controller.php for our example. In the production environment, do your coding. In your test environment, include this line and only this line:

<?php include '../../../production/application/controllers/controller.php';

This should pull the file from your production environment into your test environment. Just ensure if your hard-coding any links to make them relative as opposed to absolute. This way when it gets pulled into you test environment, it'll make reference to your testing assets and not your production ones.

This is untested as I don't have access to my CI installation right now. Let me know how it turns out.