下面,在视图目录中创建一些简单的模版。index.php文件可以用于显示index视图:
清单30 –
News
newsas$entry){?>
escape($entry{{'title'}});?>
AddNews
view.php 模版可以用于显示特定的新闻条目:
清单31
escape($this->news{{'title'}});?>
escape($this->news{{'content'}});?>
Comments
commentsas$comment){?>
escape($comment{{'name'}});?>writes:
escape($comment{{'comment'}});?>
AddaComment
escape($this->id);?>"/>
Name:
Comment:
最后,admin.php模版可以用于审批新闻条目:
清单32
escape($entry{{'id'}});?>"/>
escape($entry{{'title'}});?>
escape($entry{{'content'}});?>
Password:
为了简单起见,就使用一个带密码的表单作为访问控制机制。
放好了这些模版之后,就只要将原来放在控制器中占着位置的注释替换成几行代码。例如,
IndexController.php就变成了:
清单33
classIndexControllerextendsZend_Controller_Action
{
publicfunctionindexAction()
{
/*Listthenews.*/
$db=Zend::registry('db');
$view=Zend::registry('view');
$view->news=$db->getNews();
echo$view->render('index.php');
}
publicfunctionnoRouteAction()
{
$this->_redirect('/');
}
}
?>
组织好所有的东西之后,应用的首页的完整的业务逻辑就被减至四行代码。AddController.php还要再
处理一下,需要更多的代码:
清单34
classAddControllerextendsZend_Controller_Action
{
functionindexAction()
{
$this->_redirect('/');
}
functioncommentAction()
{
/*Addacomment.*/
$filterPost=newZend_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");
}
functionnewsAction()
{
/*Addnews.*/
$filterPost=newZend_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
classAdminControllerextendsZend_Controller_Action
{
functionindexAction()
{
/*Displayadmininterface.*/
$db=Zend::registry('db');
$view=Zend::registry('view');
$view->news=$db->getNews('NEW');
echo$view->render('admin.php');
}
functionapproveAction()
{
/*Approvenews.*/
$filterPost=newZend_InputFilter($_POST);
$db=Zend::registry('db');
if($filterPost->getRaw('password')=='mypass'){
$db->approveNews($filterPost->getRaw('ids'));
$this->_redirect('/');
}else{
echo'Thepasswordisincorrect.';
}
}
function__call($action,$arguments)
{
$this->_redirect('/');
}
}
?>