cakephp 學習1

來源:互聯網
上載者:User

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 />

   

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.