CodeIgniter has been my PHP framework of choice for a while now. I’ve churned out numerous sites based on the framework and even have my own CMS built on CodeIgniter.
I have tried out other frameworks (CakePHP, Symphony, Solar) but still came back to CodeIgniter due to its flexibility, extensive documentation and portability. Everything was fine and dandy until I decided to convert one of my (non-critical) internal apps to Rails just to try out the framework. I went from zero Rails experience (bar all the screencasts) to a fully working (small) app in three days.
Granted, my knowledge of CodeIgniter came in handy (in the end all MVC frameworks are the same at heart) but Rails has got so much convenience built-in that development is a breeze. Also, ActiveRecord (a true ORM implementation and radically different from the CodeIgniter flavour) is an absolute dream.
Fast forward a couple of weeks and back “in the real world of shared hosting accounts” here I am back to coding with CodeIgniter and it’s a real drag. The issues:
- CodeIgniter validation is long-winded and after using the Rails one-liners in the models for validation this is becoming a chore.
- ActiveRecord in CodeIgniter is really nice to use but after my experience with a true ORM and the ease of manipulating data with it I’m sold.
- CodeIgniter requires some code which (in retrospect) is actually redundant. Having to assign views manually for every controller method is a case in point.
However, I’m pleased to say my preference for CodeIgniter is back and stronger after a couple of tweaks to try and solve some of these issues. The launch of version 1.6.0 of codeigniter also came at the right time as some of the tweaks depend on features previously absent from CI.
The changes:
- Ditched OBSession: I have been using OBSession as the session library instead of that built into the CI core. I had a lot of issues prior to the switch. However the new version has some new features built-in (notably flash variables) and after a couple of days testing the applications seem to be working fine.
- Extended the Validation Class: I ran into this post and the included library was really helpful in cutting down the number of steps involved in validation.
- ActiveRecord: There’s an ORM implementation of ActiveRecord in the CodeIgniter Wiki and it basically suited my needs but its performance was not consistent. It did work on my Slicehost VPS but not on the MediaTemple GridServer or a WHM dedicated box. However, I didn’t bother to troubleshoot this after seeing the size of the objects being loaded.
I then decided to check out CakePHP but this post pointed out that the cakePHP ORM does not use objects entirely, like Rails (and the CI mod) do.
So I made a version of the CakePHP ORM for CI and it’s working fine for me. Problem solved. - Helpers: I also added a couple of custom helpers (like the h helper in rails for escaping data).
- Views: My view folder is organized like the Rails view folder with a folder for each controller and a view file for each method named the same as the method. I extended the controller (created a library called MY_Controller) and all the application controllers extend this. If a view file exists, it is loaded into the main application layout (just like in Rails). However a view file is not required for every controller.
I still have to call a render() method at the end of every controller though.
I am still tweaking this and as soon as all the bugs are ironed out I’ll show some code.
So in a nutshell, Rails made my PHP better! Ironical?
Update: I just tried out CakePHP again (for the first time after coming back from Rails) and it’s the closest PHP could come to Rails. I should use it on an actual project to get a real feel for it.
No related posts.
Yeah, I’m in the same position now, as you used to be. I’ve been doing PHP development for 7 years and I’ve been using CI intensively (I know it inside out). But now I’ve been sold to Rails and though I’m still building PHP applications (and probably I will keep build PHP apps forever, cause the market wants them), I need the fast development, the DRY and the KISS approach, the convention-over-configuration setup and the ease of use of Rails. CI doesn’t cut it…
I’ll probably give Cake (though I’m concerned about its models – I’ve seen some bad implementation that ruined DB access) and Symphony and see which one provides what I need.
How about you – did you stick with CI?