下面,在视图目录中创建一些简单的模版。index.php文件可以用于显示index视图:
清单30 –
<html>
<head>
<title>News</title>
</head>
<body>
<h1>News</h1>
<?php foreach ($this->news as $entry) { ?>
<p>
<a href="/view/<?php echo $this->escape($entry{{'id'}}); ?>">
<?php echo $this->escape($entry{{'title'}}); ?>
</a>
</p>
<?php } ?>
<h1>Add News</h1>
<form action="/add/news" method="POST">
<p>Title:<br /><input type="text" name="title" /></p>
<p>Content:<br /><textarea name="content"></textarea></p>
<p><input type="submit" value="Add News" /></p>
</form>
</body>
</html>
view.php 模版可以用于显示特定的新闻条目:
清单31
<html>
<head>
<title>
<?php echo $this->escape($this->news{{'title'}}); ?>
</title>
</head>
<body>
<h1>
<?php echo $this->escape($this->news{{'title'}}); ?>
</h1>
<p>
<?php echo $this->escape($this->news{{'content'}}); ?>
</p>
<h1>Comments</h1>
<?php foreach ($this->comments as $comment) { ?>
<p>
<?php echo $this->escape($comment{{'name'}}); ?> writes:
</p>
<blockquote>
<?php echo $this->escape($comment{{'comment'}}); ?>
</blockquote>
<?php } ?>
<h1>Add a Comment</h1>
<form action="/add/comment" method="POST">
<input type="hidden" name="newsId"
value="<?php echo $this->escape($this->id); ?>" />
<p>Name:<br /><input type="text" name="name" /></p>
<p>Comment:<br /><textarea name="comment"></textarea></p>
<p><input type="submit" value="Add Comment" /></p>
</form>
</body>
</html>
最后,admin.php模版可以用于审批新闻条目:
清单32
<html>
<head>
<title>News Admin</title>
</head>
<body>
<form action="/admin/approve" method="POST">
<?php foreach ($this->news as $entry) { ?>
<p>
<input type="checkbox" name="ids{{}}"
value="<?php echo $this->escape($entry{{'id'}}); ?>" />
<?php echo $this->escape($entry{{'title'}}); ?>
<?php echo $this->escape($entry{{'content'}}); ?>
</p>
<?php } ?>
<p>
Password:<br /><input type="password" name="password" />
</p>
<p><input type="submit" value="Approve" /></p>
</form>
</body>
</html>
为了简单起见,就使用一个带密码的表单作为访问控制机制。
放好了这些模版之后,就只要将原来放在控制器中占着位置的注释替换成几行代码。例如,
IndexController.php就变成了:
清单33
<?php
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
/* List the news. */
$db = Zend::registry('db');
$view = Zend::registry('view');
$view->news = $db->getNews();
echo $view->render('index.php');
}
public function noRouteAction()
{
$this->_redirect('/');
}
}
?>
组织好所有的东西之后,应用的首页的完整的业务逻辑就被减至四行代码。AddController.php还要再
处理一下,需要更多的代码:
清单34
<?php
class AddController extends Zend_Controller_Action
{
function indexAction()
{
$this->_redirect('/');
}
function commentAction()
{
/* Add a comment. */
$filterPost = new Zend_InputFilter($_POST);
$db = Zend::registry('db');
$name = $filterPost->getAlpha('name');
$comment = $filterPost->noTags('comment');
$newsId = $filterPost->getDigits('newsId');
$db->addComment($name, $comment, $newsId);
$this->_redirect("/view/$newsId");
}
function newsAction()
{
/* Add news. */
$filterPost = new Zend_InputFilter($_POST);
$db = Zend::registry('db');
$title = $filterPost->noTags('title');
$content = $filterPost->noTags('content');
$db->addNews($title, $content);
$this->_redirect('/');
}
function __call($action, $arguments)
{
$this->_redirect('/');
}
}
?>
因为用户在提交了一个表单之后被重定向了,所以在这个控制器里面不需要视图。
在AdminController.php中,要处理两个动作,显示管理界面和审批新闻:
清单35
<?php
class AdminController extends Zend_Controller_Action
{
function indexAction()
{
/* Display admin interface. */
$db = Zend::registry('db');
$view = Zend::registry('view');
$view->news = $db->getNews('NEW');
echo $view->render('admin.php');
}
function approveAction()
{
/* Approve news. */
$filterPost = new Zend_InputFilter($_POST);
$db = Zend::registry('db');
if ($filterPost->getRaw('password') == 'mypass') {
$db->approveNews($filterPost->getRaw('ids'));
$this->_redirect('/');
} else {
echo 'The password is incorrect.';
}
}
function __call($action, $arguments)
{
$this->_redirect('/');
}
}
?>
此文章由 http://www.ositren.com 收集整理 ,地址为:
http://www.ositren.com/htmls/548.html