Exploring Kohana as an Alternative to CodeIgniter
on 08 Sep 29
I have been developing with CodeIgniter for about 5 months now, and in that time I’ve come to enjoy this corner of PHP development. CodeIgniter allows me to take my ideas and execute them very quickly and, statistically speaking, on most any web server on the planet. However, like any healthy relationship, we are not without our arguments. To name a few: Nuking of the GET array, namespace conflicts (having to name a controller Cart and a model Kart), reading and extending PHP 4, cookie based session implementation (though database sessions are now available in the trunk) and specifying validation rules in the controller rather than the model.
Recently, a friend introduced me to a framework called Kohana (which means “swift” in Sioux). Originally a bifurcate version of CodeIgniter 1.5.x, Kohana aims to put the power back in the hands of the community which supports it rather than a company. Now at version 2.2, Kohana is ready to enter the ring and compete for our attention, and boy does it look promising. So let’s take a stroll through the world of Kohana and take a look at some of the things it has to offer and how it differs from CodeIgniter.
Owned by the Community
As I previously mentioned, Kohana’s development is entirely community driven. This means that anyone can become a developer for the project, which contrasts highly with that of CodeIgniter. Allowing those who have passion for the product to become intimately involved with its growth and progression gives those people a say in what direction that growth takes. It also increases the speed in which bugs are patched and new features are handed over to the public.
If you’ve used CodeIgniter for long enough, I guarantee you had a moment where you have thought, “How is this feature not implemented yet?” The fact is, EllisLab probably has, and rightfully should have, most of their resources tied up in the development of ExpressionEngine, their flagship product. Individuals like myself who spend a majority of their time buried inside a framework want the owners of that framework to be just as able to push it forward as we do. Kohana appears to have that.
Strict PHP 5 OOP
Unlike CodeIgniter, Kohana’s most recent version takes full advantage of PHP 5. Mechanisms such as interfaces, abstracts, singletons, visibility protection and automatic class loading permit us to code in a much more orderly and understandable way. Gone are the days of the Loader library and autoloader configs—classes are loaded automatically upon usage.
I myself have spent the better part of my programming career coding in languages like C++, C# and Java—where you are trained to take advantage of basic OOP principles. I agree with EllisLab’s reasoning in keeping CodeIgniter PHP 4 compatible; it increases their installed base. However, PHP 4’s end of life was reached on August 8th of this year. It is time to use what we’ve been given. There far too many advancements provided in PHP 5, some of which can improve the performance of the framework, to just ignore them.
For me, the most exciting feature of Kohana is its use of modules. Modules are essentially mini-apps that offer specific functionality to a parent application and are collections of controllers, models, views, libraries, helpers or configuration files. Let us assume you have a common problem across many websites—user authentication for example. Write an initial solution to the problem and place those files inside a module folder. After a bit of refining your code to make it more general, you have a drag-and-drop solution for a problem you frequently run into.
The secret to how modules work is in Kohana’s cascading architecture. The application, module(s) and system directories (think of them as layers) can all contain configuration, controller, model, library, helper and view files which all inherit and override each other dynamically. This cascade effect further encourages logical grouping of functionality.
Kohana’s models, much like CodeIgniter’s, are simply containers for you to write your own functions using the built-in Database library. This encourages very lean and efficient data access. However, Kohana also offers an ORM library which takes away a lot of the pain of repeatedly writing CRUD actions. The ORM library enables you to define relationships between multiple models and retrieve the related data via standard object properties. The lack of a decent ORM in CodeIgniter has always been unpleasant, so this is a very welcome addition.
Libraries and Drivers
Writing libraries in Kohana feels much more natural than it ever did in CodeIgniter. You will never have to do garbage like this:
$CI =& get_instance();
$result = connect($CI->config->item('search_url'), 'POST', $query);
Instead you would do something like this:
$config = Kohana::config_load('api');
$result = curl::connect($config['search_url'], 'POST', $query);
In addition to a more pleasant library experience, Kohana has a very well thought out driver system. Libraries which must interact with more than one protocol can use a different driver for each implementation. An example of where drivers have been utilized is inside the Session library; where there are drivers for native, database and cookie storage of session data.
The few weaknesses I’ve found in Kohana are primarily due to how small the project is, currently. The documentation is bit lacking, and I have occasionally found myself having to dig into the libraries themselves to figure out how something works. When starting out with a new language or framework it definitely helps to have coding samples to read, but the samples provided on Kohana’s website feel inconsistent and generally confusing. However, they do have the Learn Kohana Blog which fills in some of the blanks.
Simply put, Kohana has impressed me. The developers have taken all of the cool aspects of CodeIgniter, made them better and removed most of the suck from the lesser aspects. Don’t get me wrong, CodeIgniter isn’t leaving my toolset any time soon. ExpressionEngine 2.0 is going to be built with CodeIgniter so she still has her time and place of use. What I am saying is that any custom application work I do in the future that requires limited or no integration with ExpressionEngine will be done with Kohana.
What’s really great here is that the few weaknesses Kohana exhibits are easily fixable; all it takes is time and a bit of love from the community. I encourage you to check out the Kohana project for yourself and give it a spin. Try it. Use it. Love it. Contribute to it.
Feel free to contact me via email or twitter if you have any questions or comments about Kohana (or this post for that matter). My name is Brett and onwired.com is our domain; I’ll leave it to you to figure out my email address.
About this Author
Brett is a Web Developer here at OnWired, a web design firm in Cary, North Carolina.
When he’s not feeding his addiction for code, Brett spends his time taking pictures, reading or sitting around listening to metal.
Read Brett’s Bio.