cakephp 學習2

來源:互聯網
上載者:User

1 cakephp中,control層自動按其命名去尋找model層,比如按TaskController,則關聯Task的這個model
如果不關聯,可以這樣

<?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' => '1847192564',
'release_date' => 'December 2007'
);
$this->set($book);
$this->pageTitle = 'Welcome to the Packt Book Store!';
}
}
?>

     其中var $uses = array();則表明不關聯任何model,當然如果要關聯的話,則可以
  $uses = array ( 'ModelName1', 'ModelName2' ) ;

 上面的程式中,配合的模版index.thtml為
<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>

  則可以把$this->set($book);中的book數組的內容,自動輸出到頁面中去了.注意用這種方法的話,象
'book_title' => 'Object Oriented Programming

中的book_title,在頁面中的輸出模版是變為<?php echo $bookTitle; ?>,就是沒了中間的底線了

 

2 再來看個例子
   <?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;
}
}
}
?>

模版:
  <?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,用來儲存post過來的資料, <?php echo $form->create(null, array('action' => 'index'));?>中,

調用cakephp的formhelper工具方法,第一個參數null表明不和任何model綁定,之後的array('action' => 'index'));?>

表明是要使用controll層中的index().

echo $this->data['name'];
$this->autoRender = false;

中,$this->data['name'];輸出結果,$this->autoRender = false;在controll層中設定輸出結果,不跟view綁定輸出.

 

3 redirect跳轉
   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));
}
}

 這裡的意思是,如果有頁面提交的參數,則用redirect跳轉到userscontroller中的welcome這個action中,同時傳遞參數name.

 

 

4 可以在app裡寫個基類,比如

class AppController extends Controller {

 ....

}

放在app目錄下

   然後其他的繼承之
  class BooksController extends AppController {

...

}

 

5 cakephp的組件
   在controll中的component目錄中,寫組件 Util.php
<?php
class UtilComponent extends Object
{
function strip_and_clean ( $id, $array) {
$id = intval($id);
if( $id < 0 || $id >= count($array) ) {
$id = 0;
}
return $id;
}
}
?>

要使用時
   class BooksController extends AppController {
 var $name = 'Books';
 var $uses = array();
var $components = array('Util');

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

......

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.