Zend Framework开发入门经典教程(5)

<html> <head> <title>News Admin</title> </head> <body> <form action="/admin/approve" method="POST"> <?php foreach ($this->news as $entry) { ?> <p> <input type="checkbox" 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" /> </p> <p><input type="submit" value="Approve" /></p> </form> </body> </html>

提示:为了保持简单,这个表单用密码作为验证机制。

使用到模板的地方,你只需要把注释替换成几行代码。如IndexController.php就变成下面这样:

<?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('https://www.jb51.net/'); } } ?>

因为条理比较清楚,这个程序首页的整个业务逻辑只有四行代码。AddController.php更复杂一点,它需要更多的代码:

<?php class AddController extends Zend_Controller_Action { function indexAction() { $this->_redirect('https://www.jb51.net/'); } 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('https://www.jb51.net/'); } function __call($action, $arguments) { $this->_redirect('https://www.jb51.net/'); } } ?>

因为用户在提交表单后被重定向,这个controller不需要视图。

在AdminController.php,你要处理显示管理界面和批准新闻两个action:

<?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('https://www.jb51.net/'); } else { echo 'The password is incorrect.'; } } function __call($action, $arguments) { $this->_redirect('https://www.jb51.net/'); } } ?>

最后是ViewController.php:

<?php class ViewController extends Zend_Controller_Action { function indexAction() { $this->_redirect('https://www.jb51.net/'); } function __call($id, $arguments) { /* Display news and comments for $id. */ $id = Zend_Filter::getDigits($id); $db = Zend::registry('db'); $view = Zend::registry('view'); $view->news = $db->getNews($id); $view->comments = $db->getComments($id); $view->id = $id; echo $view->render('view.php'); } } ?>

虽然很简单,但我们还是提供了一个功能较全的新闻和评论程序。最好的地方是由于有较好的设计,增加功能变得很简单。而且随着Zend Framework越来越成熟,只会变得更好。

更多信息

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/1ae32a9c868d85e46ce827f950b95c9f.html