CakePHP: Easy As Pie

Recently, I’ve begun exploring CakePHP, an Object-Oriented PHP Framework that has support for prototyping, code-generation, and a host of other useful features. In this article, I’m going to explain why a framework like this can be beneficial, the basics of how it works, and some links to get started.

Why use CakePHP?

One of the most common complaints against PHP is that it causes people to write poorly organized, confusing coding. Until recently, PHP was a procedural language, like C or Perl, and did not support object-oriented programming. This meant that you had to have a complicated header system and a series of common functions in order to write manageable code. This was possible, but with many programmers, oftentimes your conventions were broken and this lead to bizarre behavior and potential security risks.

Enter CakePHP. As of PHP 4.2 and PHP 5, PHP supports Object-Oriented Programming (OOP). CakePHP is pretty rigid, but this means that you can rapidly prototype your applications and generate thousands of lines of codes in the matter of a few hours. It has a host of built-in features allowing you to streamline the most common work that PHP programmers do:

  1. MVC (model-view-controller) framework to easy separate the data from the way it is displayed.
  2. Authentication Functions
  3. Email Functions
  4. Form functions
  5. CRUD code generation (and admin CRUD generation)
  6. Many others

The most attractive parts to me, personally, were the authentication, rigid MVC, and code generation. Having rigid MVC means its easier for a programmer to follow what you are doing if they are familiar with CakePHP; there’s oftentimes only a couple ways of doing things.

How does it work?

Well, basically, CakePHP consists of 5 basic elements:

  • Models: interface with the database. They can inherit from predefined types, such as MySQL, MSSQL, SQLite, MS Access, etc, so you just define relationships and it does the rest!
  • Controllers: When a user requests a website, the controller says which modules to load, which views to load, and generally does all of the database querying. Its your real “action” code.
  • Views: After a controller and model have been “populated” with data, the view displays it. If you’ve heard of “smarty” or other templating systems, you will pick up on this quickly. It does not use any complicated syntax; just standard php. It is passed in a $this that contains all of the models and variables passed from the controller/model.
  • Helpers: Helpers are predefined functionality that a “view” can use, such as breadcrumbs, ajax, forms, html, etc. It keeps you from having to write too much raw javascript/html code. There are also more creative helpers (pdf, etc) available.
  • Components: Components are addons for the controllers, such as the “Auth” component that handles user authentication for you. VERY useful!

How do I get started?

Getting started is easy:

  1. Go to, download CakePHP (I suggest version 1.2 as 1.3 is still pretty new), and upload it to your server
  2. Reconfigure its “/app/config/database.php.default” file to your mysql database. Also, rename database.php.default to database.php
  3. Once you’ve done this, I suggest you just start reading through the “book” on For version 1.2, just go to Keep in mind that they have a complete “API” reference available. Some features are not documented in the book but are documented in the API, so check there when you get stuck.
  4. If you get stuck, hire me 😉

I hope this is enough to get you started. If CakePHP is not a good fit for you, I suggest Zend Framework as your backup. It is less rigid and more full featured than CakePHP, but it requires PHP 5 and does not have code generation features. Sometimes people will use CakePHP and then call up Zend Framework on an as-needed basis. This is extremely practical, but does require your programmers to be pretty knowledgeable.



  • Share/Bookmark