整合
为了进行整合,在lib目录下创建Database.php,loadClass()就可以找到它。你的index.php文件现在就会初始化$view和$db并存储到寄存器。你也可以创建__autoload()函数自动加载你所需要的类:
  1. <?php
  2. include ‘Zend.php’;
  3. function __autoload($class)
  4. {
  5.     Zend::loadClass($class);
  6. }
  7. $db = new Database(’/path/to/db.sqlite’);
  8. Zend::register(’db’, $db);
  9. $view = new Zend_View;
  10. $view->setScriptPath(’/path/to/views’);
  11. Zend::register(’view’, $view);
  12. $controller = Zend_Controller_Front::getInstance()
  13.               ->setControllerDirectory(’/path/to/controllers’)
  14.               ->dispatch();
  15. ?>
复制代码
接下来,在views目录创建一些简单的模板。index.php可以用来显示index视图:
  1. <html>
  2. <head>
  3. <title>News</title>
  4. </head>
  5. <body>
  6. <h1>News</h1>
  7. <?php foreach ($this->news as $entry) { ?>
  8. <p>
  9.     <a href=”/view/<?php echo $this->escape($entry['id']); ?>”>
  10.     <?php echo $this->escape($entry['title']); ?>
  11.     </a>
  12. </p>
  13. <?php } ?>
  14. <h1>Add News</h1>
  15. <form action=”/add/news” method=”POST”>
  16. <p>Title:<br /><input type=”text” name=”title” /></p>
  17. <p>Content:<br /><textarea name=”content”></textarea></p>
  18. <p><input type=”submit” value=”Add News” /></p>
  19. </form>
  20. </body>
  21. </html>
复制代码
view.php模板可以用来显示选定的新闻条目:
  1. <html>
  2. <head>
  3. <title>
  4.     <?php echo $this->escape($this->news['title']); ?>
  5. </title>
  6. </head>
  7. <body>
  8. <h1>
  9.     <?php echo $this->escape($this->news['title']); ?>
  10. </h1>
  11. <p>
  12.     <?php echo $this->escape($this->news['content']); ?>
  13. </p>
  14. <h1>Comments</h1>
  15. <?php foreach ($this->comments as $comment) { ?>
  16. <p>
  17.     <?php echo $this->escape($comment['name']); ?> writes:
  18. </p>
  19. <blockquote>
  20.     <?php echo $this->escape($comment['comment']); ?>
  21. </blockquote>
  22. <?php } ?>
  23. <h1>Add a Comment</h1>
  24. <form action=”/add/comment” method=”POST”>
  25. <input type=”hidden” name=”newsId”
  26.     value=”<?php echo $this->escape($this->id); ?>” />
  27. <p>Name:<br /><input type=”text” name=”name” /></p>
  28. <p>Comment:<br /><textarea name=”comment”></textarea></p>
  29. <p><input type=”submit” value=”Add Comment” /></p>
  30. </form>
  31. </body>
  32. </html>
复制代码
最后,admin.php模板可以用来批准新闻条目:
  1. <html>
  2. <head>
  3. <title>News Admin</title>
  4. </head>
  5. <body>
  6. <form action=”/admin/approve” method=”POST”>
  7. <?php foreach ($this->news as $entry) { ?>
  8. <p>
  9.     <input type=”checkbox” name=”ids[]“
  10.     value=”<?php echo $this->escape($entry['id']); ?>” />
  11.     <?php echo $this->escape($entry['title']); ?>
  12.     <?php echo $this->escape($entry['content']); ?>
  13. </p>
  14. <?php } ?>
  15. <p>
  16.     Password:<br /><input type=”password” name=”password” />
  17. </p>
  18. <p><input type=”submit” value=”Approve” /></p>
  19. </form>
  20. </body>
  21. </html>
复制代码
提示:为了保持简单,这个表单用密码作为验证机制。

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

  1. <?php
  2. class IndexController extends Zend_Controller_Action
  3. {
  4.     public function indexAction()
  5.     {
  6.         /* List the news. */
  7.         $db = Zend::registry(’db’);
  8.         $view = Zend::registry(’view’);
  9.         $view->news = $db->getNews();
  10.         echo $view->render(’index.php’);
  11.     }
  12.     public function noRouteAction()
  13.     {
  14.         $this->_redirect(’/');
  15.     }
  16. }
  17. ?>
复制代码
因为条理比较清楚,这个程序首页的整个业务逻辑只有四行代码。AddController.php更复杂一点,它需要更多的代码:

  1. <?php
  2. class AddController extends Zend_Controller_Action
  3. {
  4.     function indexAction()
  5.     {
  6.         $this->_redirect(’/');
  7.     }
  8.     function commentAction()
  9.     {
  10.         /* Add a comment. */
  11.         $filterPost = new Zend_InputFilter($_POST);
  12.         $db = Zend::registry(’db’);
  13.         $name = $filterPost->getAlpha(’name’);
  14.         $comment = $filterPost->noTags(’comment’);
  15.         $newsId = $filterPost->getDigits(’newsId’);
  16.         $db->addComment($name, $comment, $newsId);
  17.         $this->_redirect(”/view/$newsId”);
  18.     }
  19.     function newsAction()
  20.     {
  21.         /* Add news. */
  22.         $filterPost = new Zend_InputFilter($_POST);
  23.         $db = Zend::registry(’db’);
  24.         $title = $filterPost->noTags(’title’);
  25.         $content = $filterPost->noTags(’content’);
  26.         $db->addNews($title, $content);
  27.         $this->_redirect(’/');
  28.     }
  29.     function __call($action, $arguments)
  30.     {
  31.         $this->_redirect(’/');
  32.     }
  33. }
  34. ?>
复制代码
因为用户在提交表单后被重定向,这个controller不需要视图。  在AdminController.php,你要处理显示管理界面和批准新闻两个action:

  1. <?php
  2. class AdminController extends Zend_Controller_Action
  3. {
  4.     function indexAction()
  5.     {
  6.         /* Display admin interface. */
  7.         $db = Zend::registry(’db’);
  8.         $view = Zend::registry(’view’);
  9.         $view->news = $db->getNews(’NEW’);
  10.         echo $view->render(’admin.php’);
  11.     }
  12.     function approveAction()
  13.     {
  14.         /* Approve news. */
  15.         $filterPost = new Zend_InputFilter($_POST);
  16.         $db = Zend::registry(’db’);
  17.         if ($filterPost->getRaw(’password’) == ‘mypass’) {
  18.             $db->approveNews($filterPost->getRaw(’ids’));
  19.             $this->_redirect(’/');
  20.         } else {
  21.             echo ‘The password is incorrect.’;
  22.         }
  23.     }
  24.     function __call($action, $arguments)
  25.     {
  26.         $this->_redirect(’/');
  27.     }
  28. }
  29. ?>
复制代码
最后是ViewController.php:

  1. <?php
  2. class ViewController extends Zend_Controller_Action
  3. {
  4.     function indexAction()
  5.     {
  6.         $this->_redirect(’/');
  7.     }
  8.     function __call($id, $arguments)
  9.     {
  10.         /* Display news and comments for $id. */
  11.         $id = Zend_Filter::getDigits($id);
  12.         $db = Zend::registry(’db’);
  13.         $view = Zend::registry(’view’);
  14.         $view->news = $db->getNews($id);
  15.         $view->comments = $db->getComments($id);
  16.         $view->id = $id;
  17.         echo $view->render(’view.php’);
  18.     }
  19. }
  20. ?>
复制代码
虽然很简单,但我们还是提供了一个功能较全的新闻和评论程序。最好的地方是由于有较好的设计,增加功能变得很简单。而且随着Zend Framework越来越成熟,只会变得更好