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[模型名][字段名]读出