Article
The CakePHP Framework: Your First Bite
Creating your First Application
After playing with your new application for a while -- feel free to create and delete a few notes -- you'll start to notice its obvious limitations:
- the layout is very plain, and apparently is not customizable
- notes are deleted without confirmation
- there's no validation for any data input by users
We'll now remove our scaffolding and start to develop something that's slightly more advanced. If you paid attention to the previous example, you will notice that no view files were created. That's because Cake uses predefined templates for scaffolding; in reality, you'll need a view for almost every action listed in your controller.
Furthermore, our controller had no actions, and that is also part of the scaffold magic. A hint for the action names could be seen in the scaffolded application's URLs as we added and removed notes, namely:
http://localhost/notes/
http://localhost/notes/add/
http://localhost/notes/edit/1/
http://localhost/notes/view/2/
http://localhost/notes/delete/3/
In other words, all our URLs match a common pattern: they're all written in the form /<controller>/<action>/<first_parameter>/. So we need to create at least three views for the CRUD operations -- we'll name them add.thtml, edit.thtml and view.thtml -- as well as a default view (index.thtml) to list and manage all of the notes. The "t" in these thtml files indicates that these files are Cake templates. And what about delete.thtml? This file does not need to be created; we'll see why shortly.
Before proceeding, remove this line from your NotesController class:
var $scaffold;
Viewing your Notes
The first view we should create is a list of all the notes stored in the database, which will be the default page that displays when we access http://localhost/notes/. Create a new subdirectory named notes in your /app/views/ directory, then create a new file named index.thtml inside that. This file should contain the following code:
<h1>My Notes</h1>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<?php foreach ($notes as $note): ?>
<tr>
<td><?php echo $note['Note']['id']; ?></td>
<td>
<a href="/notes/view/<?php echo $note['Note']['id']?>">
<?php echo $note['Note']['title']?>
</a>
</td>
<td><?php echo $note['Note']['created']; ?></td>
</tr>
<?php endforeach; ?>
</table>
Note that our template code is not a complete HTML document -- things like the doctype and header information for all files is also provided by the framework, and the default can of course be overridden later.
This should display a list of all the stored notes, but if you try accessing http://localhost/notes/ right now, you'll get an error saying that the action index is not defined in your controller.
The code for this action needs to be created in your controller. It simply needs to retrieve all records from your notes database table and store them in an array. Cake achieves this task in one line of code:
function index()
{
$this->set('notes', $this->Note->findAll());
}
The method set is defined in Cake's Controller class, and is also inherited by AppController, NotesController and any other controller in your application. The purpose of set is to create a variable ($notes) that will be available in your default view (index.thtml), and its syntax is $this->set(string $variable_name, mixed $value).
The value of the $notes variable is a multi-dimensional array returned by $this->Note->findAll(). findAll is a method defined in Cake's Model class, which fetches all records in the database table associated with the model. In this example, we'll access our Note model and call the method from our controller.
Assuming that your notes table has some records, the output of findAll will be something like this:
// print_r($notes) output:
Array
(
[0] => Array
(
[Note] => Array
(
[id] => 1
[title] => First note's title
[body] => Some text.
[created] => 2006-04-20 14:21:42
[modified] =>
)
)
[1] => Array
(
[Note] => Array
(
[id] => 2
[title] => Title...
[body] => body text
[created] => 2006-04-20 17:22:23
[modified] =>
)
)
)
As I mentioned before, this output is accomplished with only one line of code. CakePHP dramatically reduces the amount of repetitive and boring code required in your apps, thanks to its efficient built-in classes and intuitive conventions.

Creating our first view
We proceed similarly to view a single note. First, we need a view.thtml view file in our /app/views/notes/ directory:
<h1><?php echo $data['Note']['title']?></h1>
<p><small>
Created: <?php echo $data['Note']['created']?>
</small></p>
<p><?php echo $data['Note']['body']?></p>
Then, we add the corresponding view action to our controller:
function view($id)
{
$this->Note->id = $id;
$this->set('data', $this->Note->read());
}
This method takes one parameter: the ID of the note we want to view ($id). In order to retrieve a particular note, we have to set the $id variable of our Note model to the $id parameter we passed to the method. Then we create a $data variable, which is available in our view via the set method. It contains an array returned by $this->Note->read(). read fetches only one row from our notes table, which corresponds to a particular $id.