Developing a simple organiser using CakePHP

Introduction
CakePHP is a PHP library for RAD using design patterns like
MVC, Association Data Mapping like Hibernate,JPA etc.
The organiser will consist of four basic functionality:
Notes, Tasks, Contacts and Appointments.Lets get organise now.

To follow the tutorial,you need the following software and resources.
1.Netbeans IDE ver 6.5- PHP module
2.JDK version 6 or 5
3.MySQL version 5.0
4.Xampplite
5.CakePHP

Setting up the resources downloaded
Install all the executables except CakePHP.
For more information on how to set up netbeans visit (www.netbeans.org)

Creating a PHP project with the IDE

1.Select File>New Project… ,PHP >PHP Application

Click Next.
2. Set the
Project name : Organiser
Change project location to where ever

Click Next
3.Run Configuration:
Set -:
Run As: Local Web Site (running on local web server)
Check :Copy files from Sources Folder to another location.
Copy to Folder : browse to the location of xampp installation and select “htdocs” folder


Click Finish.

Setting the project up
Unzip the cake_1.2.0.7692-rc3.zip
Copy all the files in the unzip file to your project_directory/web

This is the structure for most application.
The project will be as shown:

Setting up the database for the project
In the IDE,under Source Files select file “database.php.default” as shown

Rename the file to “database.php”.
Now edit the database.php file as shown

This is the settings i used, change to reflect yours. (login ,password ,database,host).

Codes in the application
All the php source files will be placed in the structure as shown above.
Take a moment and look at the struture under /app (models, views, controllers => MVC design pattern)
All the models of the application will be placed under /app/models
All the views of the application will be placed under /app/views
All the controllers of the application will be placed under /app/controllers

Lets have the first cake:
Scripting the models:

The figure shows us the models we need to create.

Under Source Files>app/models, Right click and then select New> PHP file..
Set the filename: task
Check if the Folder is set to “web/app/models”
Edit the file to reflect as shown:

NB: CakePHP has a convention used to bake the application based on the filename and class name of the models.You must follow the naming as i have done to make it work.Go to http://www.cakephp.org for more information on the conventions.

Create the other three models as we did above.
Edit appointment.php to reflect

Edit note.php to reflect as shown below

Edit contact.php to reflect as shown below

What just happened?
We just created four models to reflect our tables in the database.
Each model inherits from AppModel.This is a class from CakePHP to help model the tables.
Since we are following the conventions, CakePHP will get all the attributes from the tables for each model for us.

Scripting the controllers:
Every model needs a controller and the basic CRUD will be generated at runtime for us via CakePHP scaffolds.

Under Source Files>app>controllers, Right click and select New>PHP file..
Set the filename: notes_controller
Make sure that the folder is set to web/app/controllers
Edit the file to reflect as shown below.

Create the other three controllers with filenames(contacts_controller, notes_controller, appointments_controller).
Edit the contacts_controller.php file to reflect as shown below

Edit the appointments_controller.php file to reflect as shown below

Edit the tasks_controller.php file to reflect as shown below

What just happened?
We just created four controllers to manage and control our models.Open any of the controller files
,you realise there are no methods.You only see two variables.
var $name=’Appointments’ for the appointment controller.
var $scaffold;
The variable declare above does the trick. This informs CakePHP that we want a basic CRUD operation and so should take care of it for us.
What CakePHP does is to bake a read, create, update, and delete operation for all the models that have the variable $scaffold declare.What we get is a well bake cake ready to be eaten.

Testing
Open your browser and Enter this url : http:localhost/Organiser/contacts

You will notice that the page has “CakePHP : The rapis application development” header.

Adding a home page for the application
Create an empty file with filename home.ctp into app/views/pages.That is where pages without controllers are kept.
Edit to as show below:

Changng the default layout
Create an empty file with filename default.ctp into app/views/layouts.
Edit the file to reflect:

Add a css to help in laying out the pages well.
Get the file stylesheet and create a css file with name stylesheet into folder /app/webroot/css.

Running the final project
After all this alteration the final page.

Summary

We just created a simple organiser in a short time.

~ by frankappiahnsiah on October 5, 2008.

4 Responses to “Developing a simple organiser using CakePHP”

  1. any complete tutorial?

  2. In my project, it’s not running properly.
    Why?

Leave a Reply