Why I like CakePHP

A couple of months ago I began investigating php frameworks to see if I could improve my efficiency in developing rich web sites. I had tinkered with Ruby on Rails, but I didn’t have the time to learn a new framework and a new language at the same time. I realized that if I was going to make a good use of my time, I needed to begin using a framework in a language that I already knew.

I began looking at the different php frameworks and found this post on h3rald.com comparing Rails inspired frameworks. His article also points framework-seekers towards some other good frameworks such as Symfony, PHP on Trax, Code Igniter, Biscuit, and Live Pipe. I was most interested in CakePHP and Code Igniter, although PHP on Trax also caught my attention.

I decided to go with Cake, because it uses the MIT lisence and appeared to have the largest userbase. Cake’s Google Groups discussions appeared to be pretty active so I knew I could get support if I needed it. I had also read some other posts by web-guru Jonathan Snook and his experience with CakePHP. The only complaint that I saw from reading his blog was the lack of great documentation, otherwise he seemed pleased with the framework. So I decided to jump in.

My experience with CakePHP hasn’t been all roses. I’ve had my frustrations, mostly with lack of parameter details in the API documentation. However, the frustrations are far outweighed with all of the “Holy Cow!”s and “I can’t believe Cake already does that for me!”s. So, here are some of the reasons for Why I like CakePHP.

#1 – Cake’s Model does all of your CRUD for you

In past web projects, I had developed my own MVC web applications in which I would program by hand Data Access Objects. These objects would perform all of my Create Read Update and Delete operations, or CRUD operations. I always thought to myself that there had to be a better way. This always took forever to do, but it usually paid off in the end as it kept my database fairly decoupled from the rest of my applications.

Cake’s Model handles all CRUD operations automagically, and it does it well. All you have to do is define relationships between your models (or database tables). Although it might take a little while to understand the differences between belongsTo, hasOne, hasMany, and hasAndBelongsToMany relationships, there is a huge return on investment.

Some argue that the naming conventions are too restrictive, but I see it differently. I think it provides good structure for defining a schema as you work in a team. I have found the naming convention and relationship definitions to be a great asset. Reading the relationships between the tables provides good documentation on how your database schema is set up. You don’t have to spend lots of time trying to decipher someone else’s crazy SQL queries to figure out what’s going on.

#2 – Cake’s Scaffolding helps in prototyping

The faster you can get a client a prototype, the better off your project will be. CakePHP’s scaffolding provides just that. It allows you to set up your database model, define the relationships, and then automatically have screens listing records, providing create and edit forms, and record deletion capabilities. It even dynamically responds to changes made to the database. This has allowed us to check something off with a client, get quick change requests made, and then begin solidifying the project.

#3 – Cake makes building AJAX applications easy

CakePHP integrates very well with Prototype.js and Scriptaculous to help in creating rich internet applications very quickly. If you’ve never used Prototype.js, or Scriptaculous libraries, you are in for a treat. Prototype actually makes JavaScript programming fun. It abstracts browser-specific junk away from your code, so that you can get to the important code quickly. Scriptaculous integrates with Prototype to do awesome html animation effects. CakePHP provides ajax and javascript helper classes to help write the javascript code for you.

Helpful links to get you started: Prototype documentation, more Prototype documentation, Scriptaculous documentation, CakePHP AJAX Tutorial

#4 – Cake gives you pretty URLS

The CakePHP framework gives you a pretty url structure. Now you don’t have to waste time routing your mod-rewrites to your php scripts. Cake has a default url structure of {controller_name}/{action}/[{param1},{param2},etc.] . So, for example you might have a url of http://example.com/users/edit/1 which would bring you to your edit page for your user with id of 1. If you don’t like the structure, then you can use the routes config file to create custom routing.

#5 – Cake writes code for you with the bake script

Setting up your models, views, and controllers couldn’t be any easier. Cake provides a bake script that you run from the shell which builds your php files for you. It will even write the code that produces your scaffolds views, creates, and edits. This provides you with a starting ground from which to edit and customize from the scaffold. This saves you a ton of time, as you don’t have to start from scratch.

There are even more reasons for why I like CakePHP, but I’ll have to save those for another day.

7 thoughts on “Why I like CakePHP”

  1. How do you use the bake script? I have found several articles talking about how nice it is, but none saying how to use it!

  2. Susan, to run the bake script, you need to be able to run php from the command/shell prompt.

    Navigate to the directory just above the ‘app’ and ‘cake’ directories and run:

    php cake/scripts/bake.php

    The script should then walk you through the rest of the process.

  3. I think Cake is very descent for getting rapid prototypes of applications going really quickly.

    It does require getting used to the overall syntax and the way the model works and creation of all the views, controllers and so on.

    I am hoping to be able to integrate this in my future work much more, especially with Ajax functionality.



  4. @Blake:
    I’m personally looking at cakePHP because RoR isn’t an option yet for a lot of what managers like to call “enterprise” applications. If it’s not proven yet in their minds, then it’s not an option. PHP has proven scalability and plenty of support. I’ve read countless “RoR is ready for prime time” versus “nu-uh” flame threads, but the only thing that matters is what the boss thinks. RoR is still a risk.

  5. I meeted to cake one month ago. it’s very easy, rapid and disciplined.

    but, cakephp seems to nazi camp :))

    The table name must be ….
    The controller name must be …
    The model name must be …
    Table relations must be

    must be must be must be… its very crazy :S

    altogether i like cakephp..

  6. It has been a while since I’ve used Cake, but I vaguely remember that it does have options to specify in its model definitions alternate names, relationships, etc.

    I know Ruby’s ActiveRecord and DataMapper provide these options and it appears that Cake has been attempting to follow their lead.

Comments are closed.