CakePHP 1.3 - CakeTooDoo task list example ========================================== Public domain ******************************************************************************** ### Preparing directory and files 0. Prepare cakephp directory : cd /var/www/htdocs tar xf ~/cakephp-cakephp-1.3.12-0-g96a8d97.tar.gz mv cakephp-cakephp-8236c7e/ CakeTooDoo 0. Change app/tmp owner and all it's subdirectories : chown -R apache CakeTooDoo/app/tmp 0. Change Security.salt and Security.cipherSeed values in app/config/core.php: Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi'); Configure::write('Security.cipherSeed', '76859309657453542496749683645'); ******************************************************************************** ### Preparing Database 0. Create a database : CREATE DATABASE caketoodoo; 0. Create a table with plural name and a primary key named 'id' : USE caketoodoo; CREATE TABLE tasks ( id int(10) unsigned NOT NULL auto_increment, title varchar(255) NOT NULL, done tinyint(1) default NULL, created datetime default NULL, modified datetime default NULL, PRIMARY KEY (id) ); 0. Rename database.php.default to database.php : mv app/config/database.php{.default,} 0. edit app/config/database.php , remove $test array and change $default array: class DATABASE_CONFIG { var $default = array( 'driver' => 'mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'root', 'password' => '', 'database' => 'caketoodoo', 'prefix' => '', //'encoding' => 'utf8', ); } 0. Check for errors http://localhost/CakeTooDoo ******************************************************************************** ### Writing Model 0. Create a file, named same as database table name, but singular, in app/models/task.php : ******************************************************************************** ### Writing Controller 0. Create a file, named same as database table name (plural) with _controller.php, in app/controllers/tasks_controller.php : ******************************************************************************** ### Writing View 0. Add a method named index to TasksController in app/controllers/tasks_controller.php : set('tasks', $this->Task->find('all')); } } ?> 0. Create a directory, named same as database table (plural) in app/views/ : mkdir app/views/tasks 0. Create a file, named same as method name in controller with .ctp extension (Cake Template Page), in app/views/tasks/index.ctp:

Tasks

There are no tasks in this list
Title Status Created Modified Actions
0. Check output http://localhost/CakeTooDoo/tasks/index ******************************************************************************** ### Adding a New Task 0. Add a new action and $helper variabe to app/controllers/tasks_controller.php: var $helpers = array('Html', 'Form'); function add() { if (!empty($this->data)) { $this->Task->create(); if ($this->Task->save($this->data)) { $this->Session->setFlash('The Task has been saved'); $this->redirect(array('action'=>'index'), null, true); } else { $this->Session->setFlash('Task not saved. Try again.'); } } } 0. Create a new file in app/views/tasks/add.ctp : create('Task');?>
Add New Task input('title'); echo $form->input('done'); ?>
end('Add Task');?> link('List All Tasks', array('action'=>'index')); ?> 0. At the end of app/views/tasks/index.ctp , add : link('Add Task', array('action'=>'add')); ?> 0. Check output http://localhost/CakeTooDoo/tasks/add ******************************************************************************** ### Editing a Task 0. add 'edit' action to controller in app/controllers/tasks_controller.php : function edit($id = null) { if (!$id) { $this->Session->setFlash('Invalid Task'); $this->redirect(array('action'=>'index'), null, true); } if (empty($this->data)) { $this->data = $this->Task->find(array('id' => $id)); } else { if ($this->Task->save($this->data)) { $this->Session->setFlash( 'The Task has been saved'); $this->redirect( array('action'=>'index'), null, true); } else { $this->Session->setFlash( 'The Task could not be saved.Please, try again.'); } } } 0. Create a view for edit function in app/views/tasks/edit.ctp : create('Task');?>
Edit Task hidden('id'); echo $form->input('title'); echo $form->input('done'); ?>
end('Save');?> link('Add Task', array('action'=>'add')); ?>
link('List All Tasks', array('action'=>'index')); ?>
0. add a link to 'edit' action in app/views/tasks/index.ctp : link('Edit', array('action'=>'edit', $task['Task']['id'])); ?> ******************************************************************************** ### Data Validation 0. Add data validation to model in app/models/task.php : var $validate = array( 'title' => array( 'rule' => 'notEmpty', 'message' => 'Title of a task cannot be empty' ) ); ******************************************************************************** ### Deleting a task 0. Add 'delete' action to app/controllers/tasks_controller.php : function delete($id = null) { if (!$id) { $this->Session->setFlash('Invalid id for Task'); $this->redirect(array('action'=>'index'), null, true); } if ($this->Task->delete($id)) { $this->Session->setFlash('Task #'.$id.' deleted'); $this->redirect(array('action'=>'index'), null, true); } } 0. Add a links to 'delete' action in app/views/tasks/index.ctp : link('Delete', array('action'=>'delete', $task['Task']['id']), null, 'Are you sure you want to delete this task?'); ?> ******************************************************************************** ### Viewing Completed and Pending Tasks 0. Change 'index' action in app/controllers/tasks_controller.php : function index($status = null) { if ($status == 'done') $tasks = $this->Task->find('all', array('conditions' => array('Task.done' => '1'))); else if ($status == 'pending') $tasks = $this->Task->find('all', array('conditions' => array('Task.done' => '0'))); else $tasks = $this->Task->find('all'); $this->set('tasks',$tasks); $this->set('status',$status); } 0. Add to appropriate links to app/views/tasks/index.ctp : link('List All Tasks', array('action' => 'index')); ?>
link('List Done Tasks', array('action' => 'index', 'done')); ?>
link('List Pending Tasks', array('action' => 'index', 'pending')); ?>
0. Add to appropriate links to app/views/tasks/edit.ctp : link('List Done Tasks', array('action' => 'index', 'done')); ?>
link('List Pending Tasks', array('action' => 'index', 'pending')); ?>
******************************************************************************** ### Formating Date and Time 0. Add a 'Time' helper to app/controllers/tasks_controller.php: var $helpers = array('Html', 'Form', 'Time'); 0. change 'created' and 'modified' show sction in app/views/tasks/index.ctp : niceShort($task['Task']['created']) ?> niceShort($task['Task']['modified']) ?> ******************************************************************************** ### Creating Homepage 0. Create a file in app/views/pages/home.ctp :

Welcome to CakeTooDoo

CakeTooDoo is a simple but useful application to keep a record of all the things that you need to do. Using CakeTooDoo, you can:

******************************************************************************** _BY: Pejman Moghadam_ _TAG: php, cakephp_ _DATE: 2011-11-04 11:05:35_