Get a handle on application packages
I answered a question on Stack Overflow the other day and it inspired me to write a blog post about a lesser known feature of CodeIgniter called application packages. The developer had finished an application and was wondering what the best approach would be for creating a mobile website and API while reusing configuration files, libraries, and models from their original application. Other answers involved dynamically configuring the base_url based on the requested URL which seems like an unnecessary and convoluted approach. I don’t like editing the core when a solution exists within the bounds of the framework. I proposed the usage of application packages as a simple way to create multiple applications that share resources.
For those unfamiliar, adding an application package path in CodeIgniter modifies the list of directories the Loader uses when looking for resources. More specifically, it “instructs the Loader class to prepend a given path for subsequent requests for resources.” Here is an example of adding an application package path:
$this->load->add_package_path(APP_PATH . '/third_party/package_name');
Note that application packages do not need to be located in the /application/third_party folder; they can be located anywhere on the file system.
The add_package_path() method also accepts an optional second parameter for setting whether or not you would like to load views from the package. By default, CodeIgniter will load views from a package. Setting the second parameter to false will stop that. Here is an example:
$this->load->add_package_path(APP_PATH . '/third_party/package_name', FALSE);
CodeIgniter also provides methods for removing an application package path. Here are examples of removing an application package path:
// Remove most recently added path $this->load->remove_package_path(); // Remove specific path $this->load->remove_package_path(APP_PATH . '/third_party/package_name');
The documentation on application packages is located under the section on the Loader Class, however I don’t see them utilized (or discussed) very often.
Getting back to the original problem, the solution I proposed was to create two new CodeIgniter instances (mobile site and API) and to add the main site /application folder as an application package when needed. This would provide access to any existing resources that might be needed but still allow the flexibility to create custom controllers and views tailored to the different versions of the application. The only downside I see with this approach is that it becomes difficult to override resources in a package since adding a package prepends the path to the list of directories. The workaround would be to add and remove package paths depending on the file you would like to load.
The developer also raised the question of whether or not using application paths would incur additional loading time for the end-user. I have not run any benchmarks but I would assume that since CodeIgniter is only checking an additional directory for a file the usage of application paths would have a negligible effect on application performance.