cakephp 学习1

1 安装配置

   下载1.2的版本吧,之后解压

2 在httpd.conf中设置
   <Directory "f:/myphp5/cakephp">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</Directory>

3 一个简单的例子CRUD的
  A model层
    <?php
class Task extends AppModel {
var $name = 'Task';
var $validate = array(
'title' => array(
'rule' => VALID_NOT_EMPTY,
'message' => 'Title of a task cannot be empty'
)
);
}
?>

  B CONtrol层
    

<?php
class TasksController extends AppController {
var $name='Tasks';
var $helpers = array('Html', 'Form', 'Time');

 

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);
}

 

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.');
}
}
}


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.');
}
}
}

function delete($id = null) {
if (!$id) {
$this->Session->setFlash('Invalid id for Task');
$this->redirect(array('action'=>'index'), null, true);
}
if ($this->Task->del($id)) {
$this->Session->setFlash('Task #'.$id.' deleted');
$this->redirect(array('action'=>'index'), null, true);
}
}

 }
?>
C VIEW层
    在view目录下建立子目录tasks
      index.thtml:
         <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 $time->niceShort($task['Task']['created']) ?>
</td>
<td>
<?php echo $time->niceShort($task['Task']['modified']) ?>
</td>
<td>
<?php echo $html->link('Edit', array('action'=>'edit',
$task['Task']['id'])); ?>
<?php echo $html->link('Delete', array('action'=>'delete',
$task['Task']['id']), null, 'Are you sure you want to delete this');?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<?php echo $html->link('Add Task', array('action'=>'add')); ?>
<?php if($status): ?>
<?php echo $html->link('List All Tasks', array('action'=>
'index')); ?><br />
<?php endif;?>
<?php if($status != 'done'): ?>
<?php echo $html->link('List Done Tasks', array('action'=>
'index', 'done')); ?><br />
<?php endif;?>
<?php if($status != 'pending'): ?>
<?php echo $html->link('List Pending Tasks', array('action'=>
'index', 'pending')); ?><br />
<?php endif;?>

   add.thtml:
    <?php echo $form->create('Task');?>
<fieldset>
<legend>Add New Task</legend>
<?php
echo $form->input('title');
echo $form->input('done');
?>
</fieldset>
<?php echo $form->end('Add Task');?>
<? echo $html->link('List All Tasks', array('action'=>'index')); ?>

  edit.html:
    <?php echo $form->create('Task');?>
<fieldset>
<legend>Edit Task</legend>
<?php
echo $form->hidden('id');
echo $form->input('title');
echo $form->input('done');
?>
</fieldset>
<?php echo $form->end('Save');?>
<?php echo $html->link('List All Tasks', array('action'=>'
index')); ?><br />
<?php echo $html->link('Add Task', array('action'=>'add')); ?>
<?php echo $html->link('List Done Tasks', array('action'=>
'index', 'done')); ?><br />
<?php echo $html->link('List Pending Tasks', array('action'=>
'index', 'pending')); ?><br />

   

 

你可能感兴趣的:(list,function,null,delete,action,cakephp)