CakePHP … One of the powerful frameworks used for making a web applications and making a rapid development for it using PHP . it uses commonly known design patterns like active record , association Data Mapping , Front controller and MVC (model view controller) . it’s free and open source .
Today I’ll write down a simple app. using CakePHP,
this app. will be a simple to-do list . it will make us able to view, add and edit tasks, also it will make us able to view the completed and the pending tasks .
1- We need to create a database for this app. open the MySQL client and type
CREATE DATABASE SimpleToDoList ;
2- Create a tasks table in the database by typing this few lines
USE SimpleToDoList ;
CREATE TABLE tasks (id int(10) unsigned NOT NULL auto_increment ,
title varchar(255) NOT NULL ,
done tinyint(1) default NULL ,
created fatetime default NULL ,
modified datetime default NULL ,
PRIMARY KEY (id) ) ;
Wait a minute !!! , SimpleToDoList
is a directory 😀 .
after we knew that it is a directory , move inside the directory SimpleToDoList/app/config
. You’ll find a file named database.php.default
, rename it to database.php
.
3- Open database.php
and move to the line number 73, where we’ll find an array named $default
. This array contains a database connection options. Assign login to the database user you will be using and password to the password of that user, assign database to SimpleToDoList
and the database user and the password of the user in the parameters .now your app. is ready to connect to the database . go to http://localhost/SimpleToDoList
and you’ll find that the database file config. is present and Cake is able to connect to the database .
Now we’ll make our first model and the only as we use one table (tasks don’t forget 🙂 ) . now , move to SimpleToDoList/app/models
. here, make a file named task.php
. in this file type this following code
<?php
class Task extends AppModel {
var $name = ‘Task’;
}
?>
Only this code without white spaces after and before it .
Now we’ll write our first controller which decide what to do with a request made to the web app. . we’ll need one controller which will be Tasks Controller .
Move into the directory SimpleToDoList/app/controllers
. create a file named tasks_controller.php
. type this code in the file
<?php
class TasksController extends AppController {
var $name = 'Tasks';
}
?>
Again , without white spaces after and before the code .
Now , it’s time to put the features of the app.
we need to view all tasks , so we’ll edit the code in tasks_controller.php
<?php
class TasksController extends AppController {
var $name = ‘Tasks’;
function index(){
$this->set(‘tasks’ , $this->Task->find(‘all’));
}
}
?>
Do you want me to remind you of the sentence ?!! 😀
Now move into the directory SimpleToDoList/app/views
and create a directory named tasks
inside the view directory .
Inside the tasks we just created , create a new file named index.ctp
and add the following to it :
<h2>Tasks</h2>
<?php if(empty($tasks)) : ?>
There are no tasks in this list
<?php else: ?>
<table>
<tr>
<th>Title</th>
<th>Status</th>
<th>Created</th>
<th>Modified</th>
<th>Actions</th>
</tr>
<?php foreach ($tasks as $task): ?>
<tr>
<td>
<?php echo $task[‘Task’][‘title’] ?>
</td>
<td>
<?php
if($task[‘Task’][‘done’]) echo “Done” ;
else echo “Pending” ;
?>
</td>
<td>
<?php echo $task [‘Task’][‘created’] ?>
</td>
<td>
<?php echo $task [‘Task’][‘modified’] ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
Now , point the browser to http://localhost/SimpleToDoList/tasks/index
Can you picture the output and share it with us ?!! 🙂