CodeIgniter vs. CakePHP
I almost fear putting this kind of post together as it’s bound to pull the fanatics (in the negative sense of the word) out of the woodworks. Right off the bat, let me just say that I’ve tried to be as fair and honest in this assessment and I’ve tried to keep it just to the facts while interjecting what my preferences are.
I’m pitting these two frameworks against each other but there really isn’t a clear winner. Each has its strengths and weaknesses and ultimately falls to what your preference for certain features might be.
Why compare these two?
CakePHP and CodeIgniter are quite similar in their approach on a number of things, including their support for PHP4. Any mention of one inevitably leads to someone mentioning the other.
They both attempt to create an MVC architecture which simply means they separate the (data) Model from the Controller (which pulls data from the model to give to the view) from the View (what the user sees).
They both use Routing which takes a URL and maps it to a particular function within a controller (CakePHP calls these actions). CodeIgniter supports regular expressions for routing, whereas you’ll have to wait until CakePHP 1.2 for that feature. Correction: CakePHP 1.1 supports regular expression for routing but it’s not detailed in the manual and is getting updated in 1.2.
They both support Scaffolding which is an automated way of generating a view based on the model. Scaffolding is meant for simple prototyping and CodeIgniter takes it a step further by requiring a keyword in the URL to even access the scaffolding. I’m guessing one could omit the keyword, leaving this feature essentially optional. I prefer not to have to use the keyword as I sometimes build personal projects not intended for public eyes and using a keyword would be a nuisance.
And the list goes on…
Approach to Simplicity
I believe much of CodeIgniter’s appeal is its simplicity in its approach. Most of the work is done in the controller, loading in libraries, getting data from the model, and pulling in the view. Everything is in plain sight and you can really see how things work.
CakePHP’s simplicity comes via automation (euphemistically referred to as “automagic”). It makes the coding process quicker but harder to figure out “what is going on” without popping your head into the core. For me, I like to understand how everything works and I’ve had to poke around under the hood more than once. For people just getting started, things probably look a little daunting.
Working with Models
CodeIgniter’s model handling is fairly straightfoward and basically allows you to mimic a standard SQL query with a few straightforward commands like these examples:
$query = $this->db->getwhere(‘mytable’, array(id => $id), $limit, $offset);
$this->db->select(‘title’)->from(‘mytable’)->where(‘id’, $id)->limit(10, 20);
$query = $this->db->get();
Note: the method chaining in the second part of this example is only available in PHP5.
You can also create a model object, load it in and build custom methods to handle a custom task. You’d want to do this in the model and not the controller to help isolate code into the MVC silos.
CakePHP takes a slightly different route by automatically loading in the model that matches the current controller (controllers tend to be named similarly to the models they are associated with). You can turn off this automated loading and even assign different models that should be loaded by the controller instead.
CakePHP also takes things further by establishing all the model associations for you, allowing for some really easy querying. For example, assuming I’m in a controller named post_controller, I could do the following:
I chose this particular query because it shows two different concepts. The first is the fact that I can access the Comment model via the Post model (assuming I’ve defined that association in the Post model). The second is the fact that I have a method called findAllByPostId. CakePHP allows records to be grabbed via findByX and findAllByX queries where X is equal to the field name you’re trying to find.
Where I think Cake shines is in its ability to pull in all associated data automatically. Take the following query as an example:
This query would automatically pull in all the comments associated with this Post. Really handy stuff.
When working with models, you’ll inevitably have to handle data validation. Data validation in CodeIgniter is handled via a validation class. A set of rules get defined and assigned to the validation object. The validation object automatically (I assume) validates the data passed via the URL or form. From there, you can decide how that gets handled. The validation class can also help automate some of the process of setting error messages for specific fields.
CakePHP handles its validation through the model itself in one of two ways. The first uses a single test against each field defined in a validate variable declared in the model. This works okay for simple stuff but it quickly becomes a cumbrance. Beyond simple validation, I take advantage of the beforeSave callback to perform any custom validation, invalidating any fields that fail.
It’s a toss up for me as to which one “wins”. CakePHP 1.2 will have its validation system reworked a bit to allow for more flexibility.
CakePHP handles this fairly well by using a default layout (which you can easily switch at runtime). The layout has two variables be default: title_for_layout and content_for_layout. Each action automatically links to a particular view which gets spat into place. Again, it’s the “automagic” approach. As long as you name your files a specific way, controllers automatically get linked to models and views. It’s easy enough to override all of this, too, and define your own layouts or view files. There’s no convenient way to get the generated view data, however, making custom built caching mechanisms difficult to implement.
CodeIgniter takes a very straightforward approach: like include files, almost. Each file gets loaded in and processed. There’s a templating class but it doesn’t simplify things much beyond the built-in view handling. You can mimic the CakePHP approach by always including the header and footer calls but it’s not as seamless. CodeIgniter offers hooks allowing view and caching mechanisms to be overridden and replaced with a custom system.
Out of the Box Features
CodeIgniter in my mind wins this hands down with classes for FTP, Email, File Uploading, XMLRPC, Zip encoding and more.
CakePHP on the flip side comes pretty light but tries to make up for it using the Bakery. You can, like CodeIgniter, easily drop in 3rd party classes for any features you might need. Interestingly, although I haven’t tried it, you could probably drop in many of the CI classes into CakePHP without issue.
CakePHP allows for application-wide changes to be done via the base application controller that all other controllers inherit from. Likewise, you can create global model methods using the application model file. However, you can fine tune things at the controller level using any of the controller-level callbacks (beforeFilter, afterFilter and beforeRender). Things like auto-loading helpers and components can also be specified easily at the individual controller level.
CodeIgniter allows for the auto-loading of helpers, libraries and plugins but does this application-wide.
Documentation is key to understanding any framework well enough to develop within it.
CodeIgniter has a complete list of all components with each method and property documented within. CI also has forums and a wiki which feature a lot of user-submitted code.
CakePHP, on the other hand, isn’t as well organized. The manual is starting to show its age with some sections not really going much beyond what the API offers. Because of the format of the original documentation, you can also get it in other formats such as CHM and PDF. CakePHP has the Bakery which contains user-submitted articles, components, etc. The dev team also hangs out heavily on the IRC channel (#cakephp at irc.freenode.net). Finally, there’s the CakePHP Google Group which is pretty active.
I’m a pretty pragmatic individual and I honestly feel that these two frameworks have a lot going for them. They take a much simpler approach to application development than the complexity that is something like Symfony.
I’m still personally a fan of CakePHP over CodeIgniter for much of the “automagic” that I mentioned. And it’s shortcomings have been getting addressed with each new iteration (1.2 will be a considerable leap over 1.1 but it will still be awhile before it’s released).
This comparison was based on the documentation for CodeIgniter 1.5.2 and having used CakePHP 1.1. I have specifically avoided the subject of performance due to the amount of time required to design, develop and test such a thing.