By Stickymaddness From: politicallyincorrect.za.net
Intro
This tutorial is for
anyone wanting to learn the awesome php framework known as cakephp! This guide
will show you right from the beginning how to bake a very simple app.
•
No previous
cakephp knowledge needed!
•
Newbie Ready!
This tutorial will by no
means show you how to create a full fledged app, it will however create a very
simple "app" with a Model, Controller and a View, so that you can look at the
source code and get your head around M/C/V's and how scaffolding works.
Ingredients
You'll need the following
ingredients to start your baking:
•
A PC -
typewriters won't cut it :(
•
Uniform Server (or simular)
•
Cakephp
Uniform server is one of
the most handy tools for anyone wanting to locally develop web apps. You don't need to install mysql, apache or
even, ugh, IIS! Merely download Uniform Server from http://www.uniformserver.com/
Of course you'll need to
get the latest version of cakephp, which can be found on the cakephp site .
The following isn't
essential, but I would recommend it:
•
A code editor, with decent highlighting, that supports html & php!
•
The latest
version of Firefox. IE is teh suck :(
•
A good source
of caffeine.
•
A slice of
good cake!
Getting
started
As you learn cakephp,
you'll need a place to store everything. I recommend starting by creating a
folder to store all your cakephp related files. Inside this folder you can make
subdirectories to store cakephp tutorials, the cakephp manual and cheat sheet
and a folder to keep all your install files.
Run the UniServer3_3.exe,
and have it "install" to \yourfolder\uniform. Once that's done have a look
inside that folder, but don't do anything yet. Inside you'll find:
diskw
|
Were mysql, apache and
your document root is stored.
|
| Server_Start.bat |
Starts the uniform server |
| Stop.bat |
Stops the uniform server |
| Disk Start.vbs |
Starts uniform and lets you set
which "drive" uniform should use. |
Next, create a folder
called "cake" in \yourcakefolder\uniform\diskw\www\, make sure you have the
path \yourcakefolder\uniform\diskw\www\cake. Extract the cakephp files there.
NB: Make sure you are
extracting to www\cake! The "www" folder is your document root!
Once that's done, run
Server_Start.bat. You'll see a DOS window flash some stuff and your default
browser will open. It will also create a virtual drive - w:\ which is your web
drive.
You should see a page with
purple-ish background and the Uniform logo.
Any security software you
have installed, including windows firewall, will pop up with a security warning.
NB: Be sure to unblock
apache and uniform server if it does pop up, else this won't work!
After a few seconds the
browser will redirect to http://localhost/apanel/
If however, it doesn't get
to this point and just sits on the page with the logo, there are two
possibilities (That I have encountered)
Firstly, if for some
obscure reason you are using Internet Explorer, it might just sit on the page
with the Uniform Logo. If you have the default system sounds on, you'll hear
the "clicking" sound over and over.
You can fix this with a
bit of patience, or by entering http://localhost/apanel/
Of course the best
solution for problems when using Internet Explorer, is to install Firefox! :P
Else, and secondly, if you
cannot load http://localhost/apanel/
chances are there is something blocking Port 80, which uniform needs. Shutdown
anything that could possibly be blocking Port 80. Skype for example, uses Port
80 as a secondary port, this can be changed in Skypes settings.
If you're having trouble
figuring out what is blocking the port, download an app such as ActivePorts, check
what is blocking the Port and stop it.
Preparing
to Bake
At this point you should
be at http://localhost/apanel/
This is the uniform
servers Admin Panel. For now, ignore the "Security Checklist" that might have
items in red below it.
Under the "Server Start"
heading click "Run MySQL" this of course will start MySQL.
NB: Next you'll need to
edit the htaccess file!
Open w:\www\.htaccess with
your php editor, or wordpad if you have nothing better. Change the file to contain
the following:
<IfModule
mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
The only thing different
should be the addition of the "Options +FollowSymLinks" line. Next go to http://localhost/
You should see the default
cakephp page! Don't be concerned that the page will say "Your database configuration
file is not present", we will get to that in a minute.
Now to enable baking from
any directory!
Add "w:\usr\local\php" to
the path environment variable. If you don't know how to do that, follow the
next step, if you know what you're doing, skip the next step.
For those of you lost with
the environment bit, don't worry, this is easy! Right click My Computer and
click Properties, click the advanced tab, click the "Environment Variables"
button, Under "User Variables For" select PATH and click edit. You'll see a
long line of stuff, go to the end of the line, make sure it ends with a ; and
add the following line:
w:\usr\local\php
And click ok on all the
open windows.
Baking
your app!
Time to finally get
started with some baking!! Open a DOS prompt (Start->Run->Type in cmd->Ok)
Change your directory to
W:\www\cake\cake\scripts\
Now run:
php bake.php -project
\www\cake\myproject
Where "myproject" is the
name you want to give your app. It will display the following:
___ __
_ _ ___
__ _ _
__ __ __
_ _ ___
| |__| |_/
|__ |__] |__| |__] |__] |__| |_/ |__
|___ | | | \_ |___ | | |
| |__] | | | \_ |___
---------------------------------------------------------------
---------------------------------------------------------------
Skel Directory:
W:\www\cake\cake\scripts\templates\skel
Will be copied to:
New App Direcotry:
\www\cake\myproject
---------------------------------------------------------------
Look okay? (y/n/q)
[y] >
For Look okay?
Simply hit
y and press enter.
For verbose output enter either y or n.
Y will display a log
of all the files created. I hit n to as not to have to paste the whole log in
this tutorial. You should then see the following:
Do you want verbose
output? (y/n)
[n] > n
---------------------------------------------------------------
Created: \www\cake\myproject
---------------------------------------------------------------
Creating welcome page
---------------------------------------------------------------
Creating file
\www\cake\myproject\views\pages\home.thtml
Wrote \www\cake\myproject\views\pages\home.thtml
Welcome page created
After that's completed,
open http://localhost/cake/myproject/
in your web browser. You should see
CakePHP Rapid Development
Your database
configuration file is not present.
Sweet,
"Myproject" got Baked by CakePHP!
Congratulations, you've
just baked your first project!
Creating
MySQL DB & Tables
Now to create your MySQL
database and the tables for your project.
In your web browser, open http://localhost/apanel/phpMyAdmin/
or from apanel click phpmyadmin.
This will open phpmyadmin,
another great tool. This is where all your MySQL admin will be done from.
Create a new database with
the same name as your project, in my case "myproject" This is done from the
phpmyadmin front page. In the top center of the screen there is a heading
"MySQL" directly below this is "Create new database" and a small input field,
enter the name of your database and click create.
This will create the
database and display:
No tables found in
database.
Create new table on
database myproject:
Create a new table called
"users" with 5 fields. Create the following fields: id, name, username, password,
email. If you don't know how, look below, else skip to Database Config!
The following is for
people unfamiliar with MySQL and/or phpmyadmin.
After creating your table
you will be directed to a page with a table with the following headings:
-
Field
-
Type
-
Length/Values*
-
Collation
-
Attributes
-
Null
-
Default**
-
Extra
For your first field set
the following:
-
Field id
-
Type int
-
Length/Values* 11
-
Extra auto_increment
After that click the radio
button beneath the key, to assign id as your tables primary key.
Next create fields for
name, username, password and email with the following:
-
Type varchar
-
Length/Values* 30
Leave everything else
blank, finally click save.
Database
Config
Back to the command prompt
and run
php bake.php -app
\www\cake\myproject
From W:\www\cake\cake\scripts.
You will see the
following:
___ __
_ _ ___
__ _ _
__ __ __
_ _ ___
| |__| |_/
|__ |__] |__| |__] |__] |__| |_/ |__
|___ | | | \_ |___ | | |
| |__] | | | \_ |___
---------------------------------------------------------------
Your database
configuration was not found. Take a moment to create one.
---------------------------------------------------------------
Database Configuration
Bake:
---------------------------------------------------------------
What is the hostname for
the database server?
[localhost] > localhost
What is the database
username?
> root
What is the database
password?
> root
What is the name of the
database you will be using?
> myproject
---------------------------------------------------------------
The following database
configuration will be created:
---------------------------------------------------------------
Host: localhost
User: root
Pass: ****
Database: myproject
---------------------------------------------------------------
Look okay? (y/n)
[y] > y
Creating file
\..\..\\www\cake\myproject\config\database.php
Wrote
\..\..\\www\cake\myproject\config\database.php
[M]odel
[C]ontroller
[V]iew
Please select a class to
Bake: (M/V/C)
>
Enter everything as I have
above, and read on to find out how to bake a class!
While the baking is still
wait for you to select a class to bake, use your web browser to open http://localhost/cake/myproject/.
You should see:
CakePHP Rapid Development
Your database
configuration file is present.
Cake is able to connect to
the database.
NB! If the page still says
that it cannot connect to the database or that the database configuration file
is not present, you have made a mistake and must repeat this section!!
Baking
some more!
Baking
the model
Now we will create the
model for our project. Start by entering M for model. Next enter 1 to select
User. Now it will ask you if you would you like to supply validation criteria.
Enter y.
You will now be prompted
to set validation for each field in your table. The options are:
- VALID_NOT_EMPTY
- VALID_EMAIL
- VALID_NUMBER
- VALID_YEAR
- Do not do any
validation on this field.
Pretty much self
explanatory! Below are the validation criteria that I entered.
[M]odel
[C]ontroller
[V]iew
Please select a class to
Bake: (M/V/C)
> m
---------------------------------------------------------------
Model Bake:
---------------------------------------------------------------
Possible models based on
your current database:
1. User
Enter a number from the
list above, or type in the name of another model.
> 1
Would you like to supply
validation criteria for the fields in your model? (y/n)
[y] > y
Name: id
Type: integer
---------------------------------------------------------------
Please select one of the
following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any
validation on this field.
... or enter in a valid
regex validation string.
[5] > 1
Name: id
Type: integer
---------------------------------------------------------------
Please select one of the
following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any
validation on this field.
... or enter in a valid
regex validation string.
Name: name
Type: string
---------------------------------------------------------------
Please select one of the
following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any
validation on this field.
... or enter in a valid
regex validation string.
[1] > 1
Name: id
Type: integer
---------------------------------------------------------------
Please select one of the
following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any
validation on this field.
... or enter in a valid
regex validation string.
Name: name
Type: string
---------------------------------------------------------------
Please select one of the
following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any
validation on this field.
... or enter in a valid
regex validation string.
Name: surname
Type: string
---------------------------------------------------------------
Please select one of the
following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any
validation on this field.
... or enter in a valid
regex validation string.
[1] > 1
Name: id
Type: integer
---------------------------------------------------------------
Please select one of the
following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any
validation on this field.
... or enter in a valid
regex validation string.
Name: name
Type: string
---------------------------------------------------------------
Please select one of the
following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any
validation on this field.
... or enter in a valid
regex validation string.
Name: surname
Type: string
---------------------------------------------------------------
Please select one of the
following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any
validation on this field.
... or enter in a valid
regex validation string.
Name: username
Type: string
---------------------------------------------------------------
Please select one of the
following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any validation
on this field.
... or enter in a valid
regex validation string.
[1] > 1
Name: id
Type: integer
---------------------------------------------------------------
Please select one of the
following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any
validation on this field.
... or enter in a valid
regex validation string.
Name: name
Type: string
---------------------------------------------------------------
Please select one of the
following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any
validation on this field.
... or enter in a valid
regex validation string.
Name: surname
Type: string
---------------------------------------------------------------
Please select one of the
following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any
validation on this field.
... or enter in a valid
regex validation string.
Name: username
Type: string
---------------------------------------------------------------
Please select one of the
following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any
validation on this field.
... or enter in a valid
regex validation string.
Name: email
Type: string
---------------------------------------------------------------
Please select one of the
following validation options:
---------------------------------------------------------------
1- VALID_NOT_EMPTY
2- VALID_EMAIL
3- VALID_NUMBER
4- VALID_YEAR
5- Do not do any
validation on this field.
... or enter in a valid
regex validation string.
[1] > 2
Would you like to define
model associations (hasMany, hasOne, belongsTo, etc.)?
(y/n) n
I selected 'n' because we
only have one table, so obviously we can't make any associations!!
If we had another table,
say "projects" we could have done something like - users hasMany
projects.
After this it will
display:
---------------------------------------------------------------
The following model will
be created:
---------------------------------------------------------------
Model Name: User
DB Connection: default
Model Table: users
Validation: Array
(
[id] => VALID_NOT_EMPTY
[name] => VALID_NOT_EMPTY
[surname] => VALID_NOT_EMPTY
[username] => VALID_NOT_EMPTY
[email] => VALID_EMAIL
)
---------------------------------------------------------------
Look okay? (y/n)
[y] > y
Creating file
\..\..\\www\cake\myproject\models\user.php
Wrote \..\..\\www\cake\myproject\models\user.php
Cake test suite not
installed. Do you want to bake unit test
files anyway? (y/n
)
[y] > n
Baking
the controller
To create the controller,
run the bake command again with:
php bake.php -app
\www\cake\myproject
Since this is a VERY basic
beginners guide, we won't be baking the controller interactively, which makes
this section easy as pie...er...cake...
Simply enter the table
name for controller name, and hit n when asked to bake interactively, see
below.
___ __
_ _ ___ __ _
_ __ __
__ _ _ ___
| |__| |_/
|__ |__] |__| |__] |__] |__| |_/ |__
|___ | | | \_ |___ | | |
| |__] | | | \_ |___
---------------------------------------------------------------
[M]odel
[C]ontroller
[V]iew
Please select a class to
Bake: (M/V/C)
> c
---------------------------------------------------------------
Controller Bake:
---------------------------------------------------------------
Controller name? Remember
that Cake controller names are plural.
> users
Would you like bake to
build your controller interactively?
Warning: Choosing no will
overwrite Users controller if it exist. (y/n)
[y] > n
---------------------------------------------------------------
The following controller
will be created:
---------------------------------------------------------------
Controller Name: users
---------------------------------------------------------------
Look okay? (y/n)
[y] > y
Creating file
\..\..\\www\cake\myproject\controllers\users_controller.php
Wrote \..\..\\www\cake\myproject\controllers\users_controller.php
Cake test suite not
installed. Do you want to bake unit test
files anyway? (y/n
)
[y] > n
Baking
the view
We're almost done here!
Once again run
php bake.php -app
\www\cake\myproject
This is just as easy as
creating the controller was, merely enter the Controller name and hit y for the
following two options, as below:
___ __
_ _ ___
__ _ _
__ __ __
_ _ ___
| |__| |_/
|__ |__] |__| |__] |__] |__| |_/ |__
|___ | | | \_ |___ | | |
| |__] | | | \_ |___
---------------------------------------------------------------
[M]odel
[C]ontroller
[V]iew
Please select a class to
Bake: (M/V/C)
> v
---------------------------------------------------------------
View Bake:
---------------------------------------------------------------
Controller Name? (plural)
> users
Would you like bake to
build your views interactively?
Warning: Choosing no will
overwrite views if it exist. (y/n)
[y] > y
Would you like to create
some scaffolded views (index, add, view, edit) for this
controller?
NOTE: Before doing so,
you'll need to create your controller and model classes (
including associated
models). (y/n)
[n] > y
Creating file
\..\..\\www\cake\myproject\views\users\index.thtml
Wrote
\..\..\\www\cake\myproject\views\users\index.thtml
Creating file
\..\..\\www\cake\myproject\views\users\view.thtml
Wrote
\..\..\\www\cake\myproject\views\users\view.thtml
Creating file
\..\..\\www\cake\myproject\views\users\add.thtml
Wrote \..\..\\www\cake\myproject\views\users\add.thtml
Creating file
\..\..\\www\cake\myproject\views\users\edit.thtml
Wrote
\..\..\\www\cake\myproject\views\users\edit.thtml
---------------------------------------------------------------
View Scaffolding Complete.
Smell that? Your baking is
done!!
Access
what you have baked!
Now to view your app! To
do this you simplyyou're your table name at the end of you project name in your
web browser, ie: http://localhost/cake/myproject/users
You should see
CakePHP Rapid Development
List Users
And a blank table. Click
new user and add a new user. Note that you won't be able to add a user without
the correct validation you set while baking, and it will automaticly give an
error message if you attempt to!
After that it will return
to the users "index view". As you'll see you can now edit/view/delete the newly
created user!!
Pretty awesome huh? Cake
has created all of this code for you, and this is only a very, very simple
program!!
Where
to from here?
I believe that the best
way to start learning a new language is to "tinker" with the code!
For starters I would look
at the code of the app we have created. Specifically the files in:
w:\www\cake\myproject\views\
w:\www\cake\myproject\controllers
The views folder contains
thtml files, which are html files. If you want to change what is displayed in a
particular view, the files to edit are here. For our project you will see there
is a index.thml file, the file loaded when opening /myproject/users from your
web browser. There are also files for each of the actions you can perform on
users, namely they are add, edit and delete.
The controllers folder,
contains the php code for your project. Since we only have one table, we only
have one controller - users_controller.php
There is also pages_controller.php,
but this is one of cakes files.
The users controller
contains all the functions for users, and is also responsible for populating
variables that are used in the view.
Your css files are in:
w:\www\cake\myproject\webroot\css
Your projects cake
settings are stored in
W:\www\cake\myproject\config\core.php
Of special interest is the
line :
define('DEBUG', 1);
This sets the level of
debug messages you would like to see, between 0 - 3.
A good place to progress
from here would be to have a look at the excellent tutorials here at the bakery.
There is also a #cakephp irc channel where you can get hold of some of the
cakephp guru's - Just try not to be too much of an annoying n00b! :)
Cakephp
data structures
Here's a quick tip to show
you what your cake variables contain. Start by making sure that core.php has
debug set above 0, ie: define('DEBUG', 1);
Make sure you have at
least one user in your project, if not, create on!
Next edit w:\www\cake\myproject\views\index.php,
and add the following to the bottom
<br />
<h1>
<?php
echo debug($user);
?>
</h1>
Save and close the file
and open your project in your browser. You will now see your "debug statement" For
example:
Array
(
[User] => Array
(
[id] => 2
[name] => stickymaddness
[username] => stickymaddness
[password] => stickymaddness
[email] =>
stickymaddness@gmail.com
)
)
This should give you a
good idea of how things are stored, and what you should edit if you want to
display different things!
If not, kept hacking at
the code! Look at the controller, the view and use echo debug($var) to figure
out how variables are handled in cake!
Finally
I hope you enjoyed my
tutorial, it's the first one I've written so I hope it doesn't suck too badly
;)
Comments, praise,
complaints, cake and beer can be sent to me @ stickymaddness at gmail dot com!
If you would like to post my tutorial else where, please drop me a line to let me
know where!
|