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

  1. Apache HTTPD Server (2.0 – 2.2)
  2. PHP (4.x – 5.x)
  3. 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.

cake01

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.

cake02

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:

cake03

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!

cake04

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.

cake05

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

cake06

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.

cake07

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.

cake08

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.

cake09

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:

cake10

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:

cake11

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.

cake12

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

cake13

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

cake14

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

cake15

The add wish screen

cake16

The wish index with an item

cake17

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.

 

If you liked this article, please help spread the news on the following sites:

  • Bump It
  • Blend It
  • Digg It
  • Bookmark on Delicious
  • Stumble It
  • Float This
  • Reddit This
  • Share on FriendFeed
  • Clip to Evernote