5 CodeIgniter libraries you must have
There have been multiple reasons why people still stick to CodeIgniter - small footprint, exceptional performance, wonderful documentation, nearly zero configuration and various others. Even I tried to get away from CodeIgniter, but familiarity with the framework brought me back to it again. But, this time I learnt from the shortcomings of the framework and tried to get the best of both the worlds by integrating other third party libraries to vanilla CodeIgniter setup. I have come up with the following list of 5 must have libraries which you should be using if you are developing with CodeIgniter Framework.
CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.
HMVC Modular ExtensionHMVC stands for Hierarchical model–view–controller. The basic idea is to
widgetizeyour code - turn your code into independent
modules. Once you have your code segregated into modules, it becomes super easy to reuse controllers across other controllers and methods. Another major benefit is code maintenance - if you want to modify a particular feature, you would only need to change code within that particular widget/module and rest of the code remains agnostic to the changes.
The two most commonly used HMVC frameworks are:
Extend your Models
CodeIgniter comes with a wonderful database abstraction layer which supports databases like -
ODBC. CodeIgniter's Active Record Class makes it very simple to write your database queries independent of your database type. The only downside I felt was that I have to write all the database queries again and again for every model I have. It would really help if we could have a common placeholder for all such common database APIs which can be reused across all the models.
CodeIgniter Base Model is just what I wanted. It comes preloaded with all the basic
CRUD (Create Read Update Delete) operations. It has got in built common functions for form validation along with event based observer system such as hook to your callback routine just before inserting data into database. You need to just extend your models from this base model and you are done.
Use a Template Engine
CodeIgniter comes with its own templating engine which can parse simple variables or variable tag pairs. You can use the template library or use the views entirely written in php. Whether to use php for views or use a templating engine is a topic of much debate. Some people like everything in php simply because using that way gives them more flexibility in calling any routine from their code and it lets the view pages run a little faster. However, using templates segregates the
presentation logic from
business logic so that a front end developer never has to worry about the business logic and can write templates even though he may not understand back-end code. If your website grows bigger and more and more new people join, it becomes easier to expand if you have this segregation of business and presentation logic.
There are way too many options when it comes to templates, but I zeroed down on two template engines specifically for php - Smarty and Twig. They can easily be integrated to CodeIgniter, but there are wonderful people out there who have already done the job for you -
Authentication and Authorization Library
This is where CodeIgniter lags behind other php frameworks. It does not come with an authorization library and you got to build one for yourself. An authentication library provides out of the box support for authenticating users, <authorize the users to do a particular task based on their roles, manage the roles and permissions etc. Once we have any such library, administrative tasks becomes easier and you can segregate the code based on user roles. For example, you would always like to have an admin panel which can only be accessed by site administrator, while all other users should be restricted to access this page. Similarly, a logged in user has the permission to post new blogs, while an usual visitor who is not logged in can just view the blogs. All these tasks become seamless once you use a third party authentication library such as:
Custom Routing Library
Typically there is a one-to-one mapping between a URL component and its corresponding Controller Method. However, CodeIgniter lets you define your own custom routes which can be defined in
Regular Expressions and
Wildcards. It works well for basic use cases, but if you need more detailed and controlled routes - you got to look out for other alternatives. I haven't found many Custom Routing Libraries as such, but Pigeon is an excellent routing library that supports
HTTP method based routing,
RESTful and nested routes built on top of native CodeIgniter routing mechanism.
If you find it way too comfortable to sneak peek into
php code, then you can check out source code for Bonfire which includes the very best of custom routing library - inherited from Pigeon and Laravel. More details can be found here.
This is by no means a comprehensive list of most useful CodeIgniter libraries, there are many others which you should be using as and when needed. But, this is the very basic set that you should have before starting out development using CodeIgniter.
This is what I feel is the list of 5 CodeIgniter libraries you must have, feel free to leave a comment as to what you think should be your top 5 CodeIgniter library candidtates.