Getting to know CakePHP (Part 1)
Welcome to the first of hopefully many articles based on getting to know and love CakePHP.
This article assumes a working knowledge of web development programming in general, including working with Apache, a MySQL database and most obviously, PHP.
CakePHP is a development framework similar to Ruby on Rails, although written in PHP. CakePHP is modelled on “Convention over Configuration” paradigm, meaning that there should be as little configuration as possible to enable you to very quickly set up a working website. This method of programming is often called RAD (Rapid Application Development).
Important Resources
Before we grab the source code of Cake (PHP) and get stuck in, let us first find out the requirements.
Required Setup
- Apache HTTPD Server (2.0 – 2.2)
- PHP (4.x – 5.x)
- MySQL (3.x – 5.x)
As well as the above, some kind of IDE such as Eclipse PDT or E-Text Editor is also recommended for ease of development. Assuming you have all of these set up correctly, it’s time to grab the source code of Cake from their most excellent website.
As of writing, the latest stable version of the “Cake Core” is 1.2.3.8166. The large download button shows the way, and the rest is easy. Once downloaded, extract to your configured document root.

The easiest way to get started, is to create a database using your preferred database management tool (in my case, phpMyAdmin or you can use MySQL Workbench)
For the purposes of this tutorial, I will use a very simple database, the app we will be making is a list of sorts, be it a wish list or a to-do list, whatever you want to do to expand on it will be possible with the help of future tutorials.
Requirements
- Ability to view a list of “wishes”
- Each item should include a link to the particular wish
- Each item should be editable to check off whether it has been bought/completed
- The list of wishes should order the uncompleted wishes at the top, and the completed wishes at the bottom.
A very simple set of requirements makes for a very simple and quickly made application. To start off, let’s create the database.

CREATE DATABASE `wishlist`; CREATE TABLE `wishlist`.`wishes` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `name` VARCHAR( 100 ) NOT NULL , `link` TEXT NOT NULL , `complete` BOOL NOT NULL , `created` DATETIME NOT NULL ) ENGINE = InnoDB;
And that’s all there is to creating the database and its table. Notice that the table is named as a plural. This is fundamental to creating tables which CakePHP can understand easily. Also the `created` field is an automagic field, which Cake will automatically populate on creation of a row. ‘modified’ is also another automagic field. Information on field types can be found in the CookBook.
Baking your Cake
Now we’re getting into the technical part. Open a command prompt. If you’re using Linux, I’d expect you to already know how to do this. If you’re using Windows, press the Windows Key and R on your keyboard and type “cmd” without the quotes. In OS X, press Command + Space and type “Terminal” and hit enter.
It should look something like the following in Windows:

Find the directory you installed Cake Core into, and navigate to the console directory. We are going to use the automatic baking application to create the website at light speed!

The command that has to be run is “cake bake” as you can see above. This will run the CakePHP Console application. When run with no parameters it will take you through a wizard of sorts. Use this tool to create a new directory somewhere inside your DocumentRoot.
This will dump the default system into the directory, and update its own configuration so it knows where to find the core files.
The next step, it will ask you to specify your database configuration. You can pretty much enter the default values (just hit enter at each option).
When it asks you for the database name, choose “wishlist” or whatever database you chose at the beginning.

If everything went smoothly, you should end up with the following:

Follow the on screen instructions for editing the current page and its layouts if you wish.
Now is the time to bake the models and controllers for your system. The easiest way to accomplish this is to use the built-in baking tool once more and create these quickly.

As we have already set up the Database Configuration, select M first to create the models.
Don’t forget to specify –app on the command line in order for the application to know what CakePHP project you are working with.

As you can see, CakePHP has automatically detected our only model and has given it as an option to create a model with. Select ‘y’ so that we can create validation criteria that will be included with the model.
For each field, a screen like the following will be shown, choose the type of the field for each field you have in your table. CakePHP usually does a pretty neat job of automatically choosing for itself, but sometimes it needs a hand.

In our example, CakePHP does not automatically detect the `link` field as a URL field, nor does it detect the BOOL field `complete` as a Boolean. Select 27 for the `link` field, and 4 for the `complete` field.
Select 8 for the `created` field and you should be all set.
Answer ‘n’ for the model associations question, as there are no other models to associate with in this particular instance.
When your model baking is complete, this is how it should all look:

Answer ‘n’ again to the testing question. The testing suite is pointless this early in your CakePHP career, but will come in handy once you start getting more advanced.
Next you need to bake your controllers (The actual processing part of your application). Select C on the list:

The controller section is very easy. You do not need to build your controller interactively, you only need the basic functions, and you do not need to set up admin routing for now.

Again select ‘n’ for the SimpleTest question and continue.

As long as you have selected as I have above, you should end up with something similar to this:

This is the baked index screen, which can be edited in wishlist/views/wishes/index.ctp

The add wish screen

The wish index with an item

A single wish view page
Conclusion
CakePHP is incredibly versatile in the way it works, and its configurability. You can literally make a site in a lunch break. The next tutorials will show you how easy it is to customize the app you’ve created today, to finish the remainder of the original requirements of the application, and hopefully bring you a deeper understanding on how good it is to have tasty Cake once in a while.
Robin Duckett is a 22 year old guy from Wales. You can catch him on twitter or visit his website.


Brilliant article Robin! Glad to have you writing with us!
Nice tutorial! It’s always good to have another tutorial out there that is different from the one in the manual. I’m looking forward to part 2!
Nice little introduction to Cake.
The cookbook is a godsend! Used CakePHP recently but I only dipped a toe in, seems very very good though!
Interesting article, I’ve been meaning to get into some CakePHP for a while now so decided to stop by and read the tutorial, was quite in-depth and well written. I’m a PHP developer so this could be a good useful way to cut time during projects during development. Can’t wait until the follow up CakePHP tutorials arrive Robin.
// Anthony
Great article Robin, glad to have you a part of Fuel Your Coding!
Great Robin!
Read this as I was posting it and it’s a really nice step by step tutorial! Hope to read many cool articles from you in the future :)
Good article, especially for new comers to the CakePHP world.
Interesting approach. I personally never use command line to bake some cake – I love to take everything in my hand and write the MVC files myself.
Still, thanks for sharing this useful information :) .
In a single word……it’s Marvelous……I m trying to start cakephp with it.
I suggest to use free PHP IDE Codelobster PHP Edition with special CakePHP plug-in for web developing.