This tutorial is meant for people who are already developing using PHP but want to improve their style and embrace new practices - like the MVC pattern.
I'll break the tutorial into multiple posts, as it get's bots hard to write and read the tutorial if it's long.
We'll build a Movie Trailer App - or Movee for short (love the web2.0 name, BTW) where people will be able to browse the latest movie trailers (we'll just embed YouTube vids), rate them, comment etc.
So, off we go. Download the latest stable version, extract it to your local machine and we're ready to go. For this purpose, I've extracted my into localhost/movee directory. When you extract the archive, you should get something like this
- Code: Select all
[localhost root]
/movee
/app
/cake
/vendors
.htaccess
index.php
Next, we need a database. Create a new database, call it movee (or whatever you like) and add a table called trailers that looks something like this
- Code: Select all
id - int, primary key
title - varchar(200)
description - varchar(255)
youtube_url - varchar(255)
created - datetime
Open /config and rename the database.php.default to database.php. Open it as we need to do a few tweaks to tell Cake to use the database we just created. You can just copy-paste the code, just make sure you modify the parameters to your system. The code it really self-explanatory.
- Code: Select all
<?php
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'movee',
);
}
?>
Hit http://localhost/movee and we're ready to write our first lines of code.
First, the model. Models are the one that are responsible for communicating with the database, fetching, saving and deleting data.
- Code: Select all
<?php
class Trailer extends AppModel {
var $name = 'Trailer';
}
?>
This is all it takes for Cake to know you want your model to "talk" to the trailers table in your database. The code is PHP4 style, this is kind of a downside to Cake, but the future versions will be pure PHP5.
Next we need a controller. Controllers are like glue. They process the client requests, take the corresponding model, load the data from it and pass it to the view which serve only to display the data.
- Code: Select all
<?php
class TrailersController extends AppController {
}
?>
What you can do now is go to http://localhost/movee/trailers and Cake will automatically map that URL to the controller.
BTW, you'll get an error saying there is no index action.
When your URL's point to something like http://localhost/movee/trailers, Cake assumes you want the "index" action. So let's create one. The following code goes inside the trailers_controller.php
- Code: Select all
function index() {
$trailers = $this->Trailer->find('all');
$this->set('trailers', $trailers);
}
What this does it uses the Trailer model's built in function called find and with the parameter 'all' finds all the trailers in the trailers table. (You might want to add a few rows first using your GUI/phpMyAdmin/shell tool)
The set function sends the data to the view file which we'll create now.
In the /views folder create a new folder /trailers and in it a file index.ctp
- Code: Select all
<h1>Latest trailers</h1>
<?php foreach($trailers as $trailer):?>
<b><?php echo $trailer['Trailer']['id'];?></b> <br />
<?php endforeach;?>
We're basically looping through the list of trailers here and just echoing the title. Notice you have to use the ['Trailer'] index first. I'll get to explaining this in one of the next posts.
Next up we'll create a way of adding new trailers, editing and deleting. We'll also be able to VIEW the trailer
This was really quick and you will surely have a ton of questions. Shout them here or PM me any time.
Before you ask, here are a few resources that are a must see:
Hope you want more

