I have a CI website which contains two application folders, one for the front end (users) and another for the admin user:
/applications/front/ /applications/admin/
Inside the /applications/front/helpers/ we have some classes like MY_url_helper.php, and then because we want to use this in the admin area, we have the same exact code in
/applications/admin/helpers/MY_url_helper.php – so when we modify the code, it’s in two places.
As you can imagine, this goes against SOO many rules! What’s a better way of sharing the same helpers/libraries with two applications on a single website with CI?
Well, you have a few options:
Leave things the way they are and don’t worry about “breaking the rules”. You will most likely find that, eventually, you will need a very different codebase for your admin/control panel than for your front end – so duplication becomes a non-issue as you end up writing different code.
Integrate both applications into one. This solves the duplication issue but causes others, like the need to balance each library or helper to work appropriately in both environments. My experiences with this are exposed in greater detail here, and there is a blog post by CI developer Phil Sturgeon that sums up how to implement this practice here: http://philsturgeon.co.uk/blog/2009/07/Create-an-Admin-panel-with-CodeIgniter
This is one I haven’t used but looks promising: Packages.
Regardless of the recommendation to use the
/third_partydirectory, this can be any directory you choose.The full details are explained on the docs for the Loader: http://codeigniter.com/user_guide/libraries/loader.html
And once again, here is another post by Phil Sturgeon about it that may help you understand how it works: http://philsturgeon.co.uk/blog/2010/04/codeigniter-packages-modules
The idea here is that you would have a third “application” folder (your “package”) which would be used as a fallback for loading resources, while allowing you to override the file by placing a matching one in either of the other applications, so you get the benefit of shared resources with the flexibility of being able to override them *.
* This is the assumption I am under from reading the docs, I have not tested this so there may be more to it.
You would probably want to place the package directory next to your other two application folders, and reference it in
MY_Controller::__construct()like:I haven’t used yet packages myself, but after writing this post – I’m very tempted to check it out for my next project.