Article
The CakePHP Framework: Your First Bite
Tasting the Batter
As an example, let's now look at building a simple memo application that allows you to add, edit, display and delete personal notes, which are stored in a MySQL database. In order to try this yourself, you'll need to download the latest stable version of CakePHP and ensure that your development environment meets the following requirements:
- has access to a web server like Apache, although others like IIS and Lighttpd are supported
- has the ability to rewrite URLs (e.g. using the mod_rewrite module for Apache -- by default CakePHP comes with a .htaccess file to handle this)
- has MySQL, or a similar database server installed, and you have the necessary privileges to create a new database; other solutions like PostgreSQL or SQLite should work, although MySQL is recommended (this example will assume that you are using MySQL)
- has PHP version 4.3 or above; CakePHP seamlessly supports both PHP4 and PHP5
After downloading the CakePHP package, extract its contents to the document root directory of your web server, or one of its subdirectories. For this example, I'll assume that your CakePHP application is installed in the document root directory and that it's accessible at http://localhost/.

Confirming configuration of CakePHP
If you now try to access your application, the CakePHP default page will be displayed, warning you that a database connection could not be established. We need to create a new MySQL database named memo that's accessible by a user named memouser, and a new table named notes:
CREATE TABLE notes (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50),
body TEXT,
created DATETIME DEFAULT NULL,
modified DATETIME DEFAULT NULL
);
Note how the table uses the plural notes. Now, edit your database configuration file (/app/config/database.php.default) as follows, and save it as database.php (yes: the name matters!):
<?php
class DATABASE_CONFIG
{
var $default = array('driver' => 'mysql',
'connect' => 'mysql_pconnect',
'host' => 'localhost',
'login' => 'memouser',
'password' => 'userpassword',
'database' => 'memo' );
var $test = array('driver' => 'mysql',
'connect' => 'mysql_pconnect',
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'project_name-test');
}
?>
If you refresh the default page, CakePHP will notify you that the database is now accessible.
CakePHP is now configured properly, and we can turn to the development of our application. The framework offers a useful Rails-inspired feature called scaffolding, which basically allows the creation of an interface that's able to perform Create, Read, Update and Delete (CRUD) database operations with only a few lines of code. This is particularly useful when you want a particular area of your application to be available for testing purposes quickly, and you don't want to spend time coding it properly -- yet.
To create a scaffolded version of our memo application we need to create two very basic files: a controller and a model.
Create a file named note.php (again, the name matters -- notice how the file and the class defined here are the singular note of the database table notes) and save it in your /app/models/ directory. You need only include the following lines in it:
<?php
class Note extends AppModel
{
var $name = 'Note';
}
?>
Similarly, create a notes_controller.php file containing the code below, and place it in /app/controllers/.
<?php
class NotesController extends AppController
{
var $name = 'Notes';
var $scaffold;
}
?>
The $scaffold variable will trigger CakePHP's default scaffolding behavior: a fully-functional Notes section will be created, and will be accessible at http://localhost/notes/.
That's all there is to it. You are now able to create, update, delete and display your notes with (literally) five lines of PHP code!

The default edit view for a CakePHP application
This is an old trick, and if you've ever read a beginners' tutorial to Ruby on Rails you probably won't be too amazed; however, it's nice to know that a formerly Rails-only feature has been ported to PHP.