cakephp 學習小結 5

來源:互聯網
上載者:User

1 頁面模版
  來看例子,books_controller.php
   <?php
class BooksController extends AppController {
var $name = 'Books';
var $uses = array();
function index() {
$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!';
}
}
?>
  index.thtml
  <h2>Book Store</h2>
<dl>
<dt class="header"><?php echo $bookTitle; ?></dt>
<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>
然後做個default.thtml,放在
  app\views\layouts下
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
<?php echo $title_for_layout; ?>
</title>
<?php echo $html->css('stylesheet'); ?>
</head>
<body>
<div id="container">
<div id="header">
<h1><a href="http://www.packtpub.com/">PACKT PUBLISHING
</a></h1>
</div>
<div id="content">
<?php echo $content_for_layout; ?>
</div>
<div id="footer">
COPYRIGHT 2008 @ PACKT PUBLISHING
</div>
</div>
</body>
</html>
   其中<div id="content">
<?php echo $content_for_layout; ?>
</div>
  部分就是輸出實際內容的部分

而css,放在app\webroots\css下
 

2 建立自訂helper
    <?php
class FormatHelper extends AppHelper {
function hyphenateISBN( $isbn ) {
return substr($isbn, 0, 5).'-'.
substr($isbn, 5, 2).'-'.
substr($isbn, 7, 2).'-'.
substr($isbn, 9, 1);
}
function shortenDate( $date ) {
return substr($date, 0, 3).' '.
substr($date, -4);
}
}
?>
  放在app\views\helpers下format.php
在控制器中
  var $helpers = array('format');
在模版中
   <?php echo $format->shortenDate($book['release_date']); ?>

3 formhelper
  比如
   <?php
echo $form->create(null, array('url' => '/books/view', 'type' =>
'post'));
echo $form->input('Book.book_title', array('type'=>'text'));
echo $form->input('Book.author', array('type'=>'text'));
echo $form->input('Book.isbn', array('type'=>'text'));
echo $form->input('Book.release_date', array('type'=>'date'));
echo $form->end('Submit');
?>
  其中 <?php
echo $form->create(null, array('url' => '/books/view', 'type' =>
'post'));
  產生的就是如下的html
  <form action="/interfaces/books/view" method="post">
 
  之後的就是
     echo $form->input('Book.book_title', array('type'=>'text'));
   其實就是產生一個文字框
   而在控制層中,可以通過這樣讀出提交的資料,就是
    $this->data['Book']['release_date']
     即通過$this->data[模型名][欄位名]讀出

 

聯繫我們

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