The blog

CakePHP vs. Code Igniter, stuff for newbies to consider when choosing a framework

If you’re new to CakePHP, Code Igniter, or MVC frameworks in general, then no doubt you’ve done some research already. In this post I’ll discuss a little about choosing the right framework, and I’ll avoid getting into a battle over which one’s a better framework. Truth is that both frameworks are great and offer a similar but differentiated approach to MVC, and the choice really depends on your application and your working preferences. (This post is mostly intended for newbies to the world of MVC frameworks.)

If you’re digging around on this topic, then you’ve probably seen a few posts discussing this topic, including:

Choosing to go with MVC

If you’re new to MVC architecture, then it may seem like the best thing you’ve ever seen or heard of. It could take you a while to wrap your head around the concepts, but no doubt you’ve already seen much of the value in the methodology.

Are you just building a blog site?

If you’re just building a blog site, then you’ve probably seen some screencasts on creating a blog in 20 minutes, or something similar to that. But when it comes to actually building a powerful blog, it would take you a long time to build in the functionality already offered by a number of open source blog apps, including WordPress, Textpattern, MovableType, and many more. You simply won’t be able to compete with the features that these already offer, especially when considering the availability of plugins, modules, and other add-ons that these systems can use.

In the end, you’re much better off using one of these systems to double as your CMS and blog.

Do you even sketch models?

If you’re uncomfortable or don’t see the value in sketching and designing models before building a database or fleshing out code, then MVC may not be right for you.

I’ve noticed that a lot of people will choose to use an MVC framework simply for templating purposes. Some choose them only for the URL schemes.

If you only need a template language, then just use a template language! Honestly, your five page website is probably better suited by using five distinct php or html files. There’s no reason to add in any overkill factor. Even classic flagship apps like Dreamweaver are totally sufficient for managing shared page elements and templates. Plus, static content in the end is always faster than any database-driven site.

If you enjoy designing models on paper or with modeling software, then MVC is right up your alley. It’ll all come together pretty easily, and figuring out how to implement your features won’t be too much of a chore. Since the best frameworks are open source, you’ll find a massive community of people who’ve had the same problems you’ll be facing. And if you can’t find anything on your problems, community members are always willing to help. Or at the least they’ll be able to point out design flaws that you hadn’t noticed, maybe even highlighting your redesigned issue as an issue that’s already been solved.

MVC isn’t just a templating language

MVC frameworks provide a set of rules for building custom applications. They’re not necessarily meant for simple templating. Code Igniter, for example, doesn’t have any strict layout/template rules built in. CakePHP and RoR allow the use of layout wrappers and shared elements. But these things can be done with simpler systems.

Perhaps all you need is Smarty, or maybe just Dreamweaver. Or maybe you just need to include() a few php files, man. If you haven’t thought thoroughly about why you’re using an MVC framework, then you’ll probably find yourself adapting your application to the framework down the road. And this isn’t what frameworks are for! You should choose the framework because it best fits your app, not the other way around.

20 Minute blog builds aren’t sufficient

Even though they can show you how to build a blog in twenty minutes, you don’t really end up with a very powerful blog. You’ll instead end up with a very simple, very rough app you can build upon. No feeds. No XML sitemap. No accounts and membership types. No swift template system. You just can’t compete with the features offered by popular blog apps without pouring countless hours into your system.

Choosing between CakePHP and Code Igniter

If you’re sure you want to use MVC, and you’re sure that it’s the best fit for your custom application, then you should consider a few things if you’re choosing between CakePHP and Code Igniter.

Modeling

If you’re big on modeling data, setting up relationships for relational databases, and you like to specify strict rules for your models, then CakePHP is probably going to win over Code Igniter.

CakePHP allows you to create your models much more intuitively, and you can apply your relationships and rules directly in the model. Plus you can ‘bake’ these components via Cake’s Console and Shell apps. Code Igniter, on the other hand, doesn’t strictly regulate models and doesn’t recommend much on the topic. This, of course, is one of the beauties of using Code Igniter: that it doesn’t require you to use models at all, and that you can build your models to appropriately handle whatever your data needs may be.

Libraries

CakePHP and Code Igniter offer many similar libraries and functionality, but they’re very different in how you use them. You should consider your preferences for working when choosing which one to use.

For example, both offer form validation libraries. Code Igniter allows you full control over your validation, but this control is built into the controller rather than the model. MVC purists (this debate can get pretty heated) shun the idea of allowing a controller to handle data validation, as anything related to the data should remain on the data layer, in our case, this is the model.

CakePHP, on the other hand, keeps the data validation within the model. The controller helps handle the form stuff, but it’s handling only the application logic, which belongs in the controller anyway.

It has been mentioned, though, that many people have simply dropped libraries to and fro between the two frameworks, so this is a possibility if libraries are influencing your decision.

Views

CakePHP offers much more flexibility and power when it comes to working with multiple views. This flexibility, though, has its limitations, as strict rules for working with views apply. Strict is still good, though, as it helps keep your applications in line and in check, especially down the road. Anyone familiar with CakePHP should be able to browser over your app’s code and get the overall gist of it.

On the other hand, Code Igniter doesn’t tell you how to handle multiple views, layout wrappers, and such. Again, this is by design and a large contributor to the beauty of CI.

Controllers

Both handle controllers similarly. You can override/extend the default application-wide controller to add in any of your own core logic that’s shared throughout your app. However, CakePHP allows controllers to call on other controllers’ actions, keeping more inline with MVC methodology. To accomplish the same with CI, you’ll have to be sure to thoroughly plan and design this functionality.

Because CI is so flexible, it’s quite easy to take your application logic in the wrong direction, at least from MVC’s point of view.

The winner

There is no winner. I love both frameworks and often have a hard time deciding between the two. I love the strictness of CakePHP to keep my apps in check and more easily scalable. But I also love the flexibility of Code Igniter, as I can have it do only the necessary work for my app.

So think hard about it; do your research. And remember to ask yourself if your website or app can simply be done with a good old reliable WordPress or some comment-delimited shared elements in Dreamweaver.

18 Responses to “CakePHP vs. Code Igniter, stuff for newbies to consider when choosing a framework”


  1. Luis Aveiga
    2009.03.25

    Thanks for the post Steve, useful & practical.

    Luis Aveiga


  2. usman
    2009.03.28

    I’ve used cakePHP out of these two, and really think its worth! Though I’ve also tried Symfony and Zend but I like cake I may give CI a try as well! Thanks for such a nice article! Keep sharing more good things …


  3. Steve
    2009.03.28

    Also take a look at KohanaPHP. It’s based on codeigniter, but it’s open source. The Menalto Gallery 3 is built on Kohana.

    kohanaphp.com


  4. myles
    2009.05.04

    CakePHP is the best.


  5. Silver Knight
    2009.06.19

    Personally, I’ma big fan of CakePHP, however, I agree 110 percent with the premise of this post. If you choose a framework simply because someone says it’s the best thing since sliced bread without considering WHY you are choosing it, then you simply set yourself up for frustration. Choose your framework or method based on careful consideration of your need, the needs of your project, and your preferred mode of working and you really can’t go wrong. Excellent post, Steve. :)


  6. Kyle
    2009.06.25

    I am the target audience of this post — a newb to MVC, shopping around for and learning about PHP frameworks. I wanted to thank you for writing this up, I found it very down to earth and insightful. Thank you!


  7. MEC
    2009.06.27

    I have to develop a small system over the internet and I am a rookie in PHP, so I need an advice about which could be the better choice for a PHP framework. The system includes users administration module, reports module, users access application level, some small process/calculations, parameters screen, forms processing/validations and so. The system must be done using PHP/MySQL and I want reuse code at the most


  8. Steve
    2009.06.29

    MEC, I’d recommend using an existing system for this, something scalable with lots of the features you need already built-in. Try drupal.org or something similar to that.


  9. Pnkaj
    2009.08.06

    Hi I am a fan of CI and work a lot with this framework but but CI lacks some things like ORM and ACL which are really necessary things to have in framework. on the other hand cakePHP is giving both functionalites inbuilt. and in CI we have to use some third party tools like ignitedrecord need to use. So again it all depends on the need of application.

    Thanks for the good post.


  10. Joseph
    2009.08.14

    Love CodeIgniter. I’m not a big fan of ORM and CI has wonderful support for Active Record queries which are really powerful.


  11. Anatolij
    2009.10.15

    Never thought, I came across this topic, cause month ago, I was trying Yii framework, its robust, and flexible (documentation says). But after getting one project on CI – found it very comfortable and easy to learn. DataMapper, FreakAuth – tolls I’ve found in project, cool once:) but general job is to embed WordPress, or place near CI, to make intranet solution with power of WP backend to write posts, and power of CI in the front. So non documented WP+CI done. I’ll write some posts latter on maybe.


  12. aifarfa
    2009.11.04

    +1 agreed “I love both frameworks and often have a hard time deciding between the two.”


  13. Max
    2009.11.18

    I have been doing research frameworks over the last few weeks and came to the conclusion that the best framework is one that is used most in commerce. I know I will hear some boo’s over this. The fact is that any framework you pick up and use will require many weeks if not years to become proficient at it. This is an investment of time you cannot recover. These are open source frameworks with similar features – it’s all about which will be used most and stick around for the long run. That being the case Zend is the choice of business. It has the backing and is not fragmented as the Zend Company has the money to keep it in the forefront of commerce.


  14. Tom
    2010.05.25

    It’s all completely personal preference, don’t let anyone tell you otherwise. Seriously. Great post. Most people do use frameworks for the wrong reasons or don’t understand that there’s reason for choosing one over the other and it’s not because one is “better” or the “winner.”

    In my opinion you use what helps you get the job done the quickest and it should pass the “bang your keyboard” test. Which is how many times you want to bang your keyboard and/or computer. It’s just about being comfortable and efficient with your code. You want to not let it get out of hand and you want to come back 6 months later and know exactly what’s going on with your code. It’s possible to have that “zen” with any framework or no framework at all too. I just find that frameworks at least give you a paradigm to follow and convention.

    For that reason, I personally chose CakePHP because it’s a bit more strict (though completely flexible and you can change conventions, I’ve used it to “take over” existing sites and use existing database schema without many problems, though I can’t say the queries were “efficient” of course).


  15. Steve
    2010.05.25

    Very well said, Tom. Thanks for the input.

    CakePHP is indeed great. I love the convenience it offers.

    Lately I’ve found myself working much more in code igniter, mostly because I have some clients using expression engine, and now that EE 2.0 is built on codeigniter, were able to combine a super-user-friendly CMS with a sweet app framework.

Leave a Reply