My First Slice Of Cake

Yes, thats right… my first ever slice of cake… not the yummy kind… the framework kind, and I have to say I’m impressed. I have this madly simple idea for a neat website, but I don’t want to commit weeks of work to it only to watch it crash and burn (potentially). So I’ve been looking around for frameworks for developing web apps… looked at Ruby on Rails… nice, but needed what seemed like an inordinate amount of effort to set up… then I came across CakePHP. What a revelation.

I feel like I’m so behind the times on developing web applications, its untrue, but I have a sneaky feeling thats about to change.

I mentioned Ruby on Rails… download Ruby, IIRC compile it, then download RubyGems (IIRC), then use that to grab Rails. Then (having read a couple of tutorials), mess around with a bunch of scripts in a language/format which was completely alien. Ok, I have no quarms about learning another language, but that takes time… something I’m short on.

Enter CakePHP… I have PHP 4.4.0 (thats one requirement checked), MySQL 4.11 (another) and a ready to run web server with mod_rewrite (thats all the requirements IIRC). Download the file, unpack it, edit the database configuration file, edit Apache’s config and I have a site up and running (Ok, it doesn’t do much, but CakePHP was up and running).

I’m not going to write a tutorial, theres already a wealth of different ones, but to give you an indication of how easy it was to get a CRUD (great acronym… short for Create Read Update and Delete) interface up and running.

Heres the scripts etc. I had to run to get a single table operational.

Firstly, create my database and database user.

create database mysample;
grant all privileges on mysample.* to mysampleuser@'%' identified by 'mysamplepass';
use mysample;
create table manufacturers (
id bigint not null auto_increment primary key,
name varchar(64)

Thats the SQL out of the way… now edit database.php in the app/config directory to provide the database connection details.

CakePHP splits the project into Models, Controllers and Views. Models are your link to the database, and typically each table in the DB will have a model associated with it. So, you create a file in the app/models directory called manufacturer.php. And you put this into it.

class Manufacturer extends AppModel
var $name = 'Manufacturer';

Then you need to specify the controller part… this is the worker of CakePHP if my understanding is correct, the requests made by users are routed to a specific controller governed by URLs, so lets say I want to add a manufacturer, I might request the page mysite.com/manufacturers/add/. This would be routed to the manufacturers_controller.php file for processing.

So, for this, we create a file called manufacturers_controller.php in the app/controllers directory.

class ManufacturersController extends AppController
var $name = 'Manufacturers';
var $scaffold;

The $scaffold variable is important at this stage as it uses CakePHP to build the CRUD interface on the fly, making for really rapid prototyping. It also means you can fiddle with your DB design on the fly… all you have to worry about changing then is the models. You don’t have to use scaffolding, you can get straight in there and provide the views element yourself, but I wanted to just bang something together quickly.

And thats it.

You have an interface that will list your manufacturers, allow you to add new ones, edit existing ones and delete the ones you don’t want. And all that from 16 short lines. The great thing about the scaffold is that once you’re happy with it, you can run a script which converts the scaffold into code which you can then customise to meet your exact requirements.

My only complaint is the naming conventions. They are covered in the documentation, but some of it isn’t totally clear. However, this small issue aside, I’m impressed. I put together a quick prototype of a portion of my idea in a few hours (mostly spent fighting with the naming conventions) that included associations (so I can add sub groups to groups, brands to manufacturers and such like) and all the forms (scaffold admittedly, but still) to manage the data.

Suffice to say, I’m going to give it a real thorough run through with my latest idea, so I’ll be sure to post how I get on with it.

Check out the CakePHP website at www.cakephp.org.

Leave a Reply


Bad Behavior has blocked 195 access attempts in the last 7 days.