CakePHP Learning 1

Source: Internet
Author: User

1. install and configure

Download version 1.2 and decompress it.

2. Set in 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. A simple example of crud's
A model layer
<? 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 layer

<? 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 ('invalidtask ');
$ 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 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 layer
Create a sub-directory tasks under the view directory
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 task', 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 task', array ('action' =>
'Index', 'Pending');?> <Br/>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.