Feb 22 2011

CodeIgniter 2.0: First Look at Reactor

by Chris

If you are a PHP programmer then you have undoubtedly heard of the CodeIgniter (CI) framework at least once. First released in 2006, by EllisLab, CI has grown into the lightweight, fully functional and easy to use application framework of choice for developers across the web. A big milestone was reached two weeks ago with the release of version 2.0 and there has never been a better time to kick the tires and take it for a spin.

Not Just Any Old Framework

There are A LOT of open source PHP frameworks on the web today. Some of them are pretty nice tools and some of them aren’t. When evaluating a framework you need to look at several factors: community, documentation, features, support options and learning curve. Depending on your individual requirements you might have some additional criteria but I think that’s a decent baseline. Let’s see how CI stands up:

Community

Last look at the forums puts registered members at 190,000. Now that’s not necessarily active users, but that’s the number of developers who thought enough of the framework to get a forum account and join in on the conversation. EllisLab places a very high premium on their developer community and a number of the additions in 2.0 were added at their request.

Documentation

I have played around with a number of different PHP frameworks and you will be hard pressed to find a framework with better documentation than CI. The users guide is complete, easy to understand and up to date with all the latest bells and whistles. Any PHP developer with a basic understanding of object oriented programming (OOP) should be able to pick things up pretty easily.

It’s worth noting that with the release of 2.0 some new sections of the users guide are still being fleshed out, but there is enough there to help get you started.

Features

This is the true question: “can the framework do what I need it to do?” This question really depends on what it is you need it to do. But CI has most of the standard framework features including model view controller (MVC) architecture, database abstraction, form validation, session handing, error handling, flexible/scalable architecture and libraries to handle tasks like uploading files, displaying event calendars, implementing carts for e-commerce sites and much more. If that’s not enough CI also lets you add any additional functionality you want/need by building your own custom library or extending core libraries that are already there.

Support Options

So you invest all this time and effort into learning a framework and building your business around it, what do you do if things suddenly stop working? While CI doesn’t offer any kind of commercial support, the community is full of folks on the forum waiting to lend a hand when necessary. There are also a number of bloggers out there who share their CI knowledge and can be tapped for help from time to time.

The other thing worth mentioning here is that CI is backed by a successful commercial company in EllisLab (makers of ExpressionEngine CMS). So it’s a pretty safe bet that it’s not going anywhere anytime soon.

Learning Curve

We’re all on a timeline, so how long will it take to get up to speed? As I mentioned under documentation, CI is very well documented and has an active and vibrant community. With that said, anyone with a fair understanding of OOP should be able to pick things up pretty quickly.

New to Version 2.0

So now that I have your attention, let’s take a look at the exciting new additions to CI in version 2.0:

2 Flavors: Core and Reactor

As I mentioned above, CI is backed by a commercial company and is used by that company to develop their commercial products like ExpressionEngine and MojoMotor. That means that the development of the framework was limited to the needs of their products and the community got a little frustrated feeling that the development of the framework had stalled over time. In true EllisLab fashion they listened and in version 2.0 have released two different lines of development: Core and Reactor.

You now have the option of working with CI Core (maintained by EllisLab) or CI Reactor (maintained by the community). Core is the version of CI that EllisLab uses and will only change when they need to make a change for their products. This version should be used by those looking for true stability and backward/forward compatibility. Reactor, on the other hand, is maintained by a team of community engineers who collectively monitor community needs/wants and incorporate new features on a more frequent basis.

Since all changes made to Core will be incorporated into Reactor and that Reactor will be the more active of the two code bases it’s recommended that everyone use Reactor from this point forward.

PHP5 Support

A common point of contention with previous version of CI was that it wasn’t native PHP5 meaning the framework was backwards compatible with PHP4 and certain PHP5 features didn’t work or were not used. With the release of 2.0 this is no longer the case. PHP4 is no longer supported and you must be running PHP 5.1.6 or higher in order to use the framework. So feel free to start taking advantage of the OOP improvements of PHP5.

Packages

Distributing code for reuse or releasing complex libraries (for things like authentication) to the community has always been a somewhat clunky process. You would typically need to copy and paste certain files into certain places within your install in order for everything to work correctly. Config files in the config folder, libraries in their respective folder, etc. CI 2.0 does away with this issue by introducing a new way of collecting and distributing code called packages.

Packages are similar in theory to modular development in that all the resources you are distributing are self contained. All packages will have their own libraries, models, helpers, config, and language files collected in their own directory which can then be placed into the application/third_party directory of the framework for use. This provides developers with a much easier way of sharing/releasing code for use by the community. This also opens the doors for more module based application development with CI.

Drivers

In previous versions of CI there hasn’t been an elegant way of creating classes that all share a common parent class and not their sibling classes (handy in certain situations). You can now accomplish this with a new type of library called a driver. Drivers allow you to create a parent class with unlimited child classes that have no access to their siblings (for things like DB abstraction). This is a nice architecture for providing the same type of functionality only using different tools or implementation methods.

Along with being able to create your own drivers, CI 2.0 identifies three drivers in the documentation: Cacheing, Database and Javascript.

Javascript Library

Saving the best for last, the item I was most excited about with this release was the Javascript Library. CI 2.0 now gives you the ability to plug your favorite javascript framework into CI using drivers so you can incorporate javascript into your apps more easily. jQuery is being distributed with the framework by default but you can be sure there will be other drivers released shortly for other javascript frameworks.

Upgrading

So far this post has been mostly about what’s new and exciting in version 2.0 but there have also been some changes to the framework that might trip up seasoned developers and those of you looking to upgrade from an earlier version. First and foremost, all CI core classes are now prefixed with CI_. This means that all controllers, models, etc. now need to extend CI_Controller, CI_Model, etc. respectively. You will want to be sure to update your code accordingly when upgrading to version 2 (this already tripped me up once).

Second, some old features and functionality have finally been removed for good like scaffolding, plugins and the validation library. Plugins never really gained much traction with CI developers and have been deprecated for some time now. The CI core CAPTCHA plugin has been converted to a helper and any plugins you have developed should be converted to helpers also. The validation library, which has been replaced for a while now by form_validation, is gone. So if you have been putting off updating your code to use the new form_validation library now is the time to do it.

Finally, a number of improvements have been made to the encryption library which will prevent you from decrypting data that you have encrypted with a previous version. To deal with this, you will have to run an update on your data using the new encode_from_legacy() method which will decode your data and return a re-encrypted string using the new method for storage.

Those are the big things to look out for when upgrading from a previous version. Visit the users guide for complete upgrading instructions.

Is that it?

This is just a quick list of my top 5 changes/additions to the framework that have been released in 2.0. There’s a lot more where that came from like new cache drivers with APC, memcached, and file-based support, new security library and over 50 bug fixes. You can see the complete list of what’s new in the change log. So what are you waiting for? Take CodeIgniter 2.0 for a spin today!