Question

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?

Was it helpful?

Solution

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.

Application "Packages"

An application package allows for the easy distribution of complete sets of resources in a single directory, complete with its own libraries, models, helpers, config, and language files. It is recommended that these packages be placed in the application/third_party folder.

Regardless of the recommendation to use the /third_party directory, this can be any directory you choose.

The following is an example of a directory for an application package named "Foo Bar". /application/third_party/foo_bar

config/
helpers/
language/
libraries/
models/

Whatever the purpose of the "Foo Bar" application package, it has its own config files, helpers, language files, libraries, and models. To use these resources in your controllers, you first need to tell the Loader that you are going to be loading resources from a package, by adding the package path. $this->load->add_package_path()

Adding a package path instructs the Loader class to prepend a given path for subsequent requests for resources. As an example, the "Foo Bar" application package above has a library named Foo_bar.php. In our controller, we'd do the following:

$this->load->add_package_path(APPPATH.'third_party/foo_bar/'); $this->load->library('foo_bar');

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:

$this->load->add_package_path(FCPATH.'my_package_name/');

I haven't used yet packages myself, but after writing this post - I'm very tempted to check it out for my next project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top