CakePHP learning 2

Source: Internet
Author: User
Tags packt

1 In CakePHP, the control layer automatically searches for the model layer based on its name. For example, by taskcontroller, the model associated with the task
If not associated, you can

<? PHP
Class bookscontroller extends appcontroller {
VaR $ name = 'books ';
VaR $ uses = array ();
Function Index (){
$ This-> set ('page _ heading', 'packt book store ');
$ Book = array (
'Book _ title' => 'object Oriented Programming
With PhP5 ',
'Author' => 'hasin hayder ',
'Isbn '=> '123 ',
'Release _ date' => 'december 2007'
);
$ This-> set ($ book );
$ This-> pagetitle = 'Welcome to the packt book store! ';
}
}
?>

VaR $ uses = array (); indicates that no model is associated. If you want to associate a model, you can
$ Uses = array ('modelname1', 'modelname2 ');

The aboveProgram, The matching template index. thtml is
<H2> <? PHP echo $ page_heading;?> </H2>
<DL>
<LH> <? PHP echo $ booktitle;?> </LH>
<DT> author: </DT> <DD> <? PHP echo $ author;?> </DD>
<DT> ISBN: </DT> <DD> <? PHP echo $ ISBN;?> </DD>
<DT> Release Date: </DT> <DD> <? PHP echo $ releasedate;?> </DD>
</Dl>

You can automatically output the contents of the book array in $ this-> set ($ book); to the page.
'Book _ title' => 'object Oriented Programming

In book_title, the output template on the page is changed to <? PHP echo $ booktitle;?>, There is no underline in the middle.

 

2. Let's look at an example.
<? PHP
Class userscontroller extends appcontroller {
VaR $ name = 'users ';
VaR $ uses = array ();
Function Index (){
If (! Empty ($ this-> data )){
// Data posted
Echo $ this-> data ['name'];
$ This-> autorender = false;
}
}
}
?>

Template:
<? PHP echo $ form-> Create (null, array ('action' => 'index');?>
<Fieldset>
<Legend> enter your name </legend>
<? PHP echo $ form-> input ('name');?>
</Fieldset>
<? PHP echo $ form-> end ('Go');?> .

 

$ This-> data, used to save post data, <? PHP echo $ form-> Create (null, array ('action' => 'index');?> Medium,

Call the formhelper tool method of CakePHP. The first parameter null indicates that it is not bound to any model, and the subsequent array ('action' => 'index');?>

Indicates that you want to use index () in the controll layer ().

Echo $ this-> data ['name'];
$ This-> autorender = false;

, $ This-> data ['name']; output result, $ this-> autorender = false; set the output result in the controll layer and do not bind the output to the view.

 

3 redirect jump
Class userscontroller extends appcontroller {
VaR $ name = 'users ';
VaR $ uses = array ();
Function Index (){
If (! Empty ($ this-> data )){
$ This-> redirect (Array ('controller' => 'users ',
'Action' => 'Welcome ', urlencode ($ this-> data ['name']);
}
}
Function welcome ($ name = NULL ){
If (empty ($ name )){
$ This-> session-> setflash ('Please provide your name! ', True );
$ This-> redirect (Array ('controller' => 'users ',
'Action' => 'index '));
}
$ This-> set ('name', urldecode ($ name ));
}
}

Here, if a parameter is submitted on the page, use redirect to jump to the welcome action in userscontroller and pass the parameter name.

 

 

4. You can write a base class in the app, for example

Class appcontroller extends controller {

....

}

Put it in the app directory

And other inheritance
Class bookscontroller extends appcontroller {

...

}

 

5 CakePHP Components
In the component directory of controll, write the util. php component.
<? PHP
Class utilcomponent extends object
{
Function strip_and_clean ($ id, $ array ){
$ Id = intval ($ id );
If ($ id <0 | $ id> = count ($ array )){
$ Id = 0;
}
Return $ ID;
}
}
?>

To use
Class bookscontroller extends appcontroller {
VaR $ name = 'books ';
VaR $ uses = array ();
VaR $ components = array ('util ');

$ Id = $ this-> util-> strip_and_clean ($ id, $ books );

......

 

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.