整合为了进行整合,在lib目录下创建Database.php,loadClass()就可以找到它。你的index.php文件现在就会初始化$view和$db并存储到寄存器。你也可以创建__autoload()函数自动加载你所需要的类: <?php include ‘Zend.php’; function __autoload($class){ Zend::loadClass($class);} $db = new Database(’/path/to/db.sqlite’);Zend::register(’db’, $db); $view = new Zend_View;$view->setScriptPath(’/path/to/views’);Zend::register(’view’, $view); $controller = Zend_Controller_Front::getInstance() ->setControllerDirectory(’/path/to/controllers’) ->dispatch(); ?>复制代码接下来,在views目录创建一些简单的模板。index.php可以用来显示index视图: <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模板可以用来显示选定的新闻条目: <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模板可以用来批准新闻条目: <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就变成下面这样: <?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更复杂一点,它需要更多的代码: <?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(’/'); }} ?>复制代码因为用户在提交表单后被重定向,这个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(’/'); } else { echo ‘The password is incorrect.’; } } function __call($action, $arguments) { $this->_redirect(’/'); }} ?>复制代码最后是ViewController.php: <?php class ViewController extends Zend_Controller_Action{ function indexAction() { $this->_redirect(’/'); } 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://blog.51cto.com/chinawl/285487