`
ythzjk
  • 浏览: 74510 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

Zend Framework实例教程2

阅读更多

最后,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越来越成熟,只会变得更好。

 

更多信息

 

这个教程只是讨论了ZF表面的一些功能,但现在也有一些其它的资源可供参考。在http://framework.zend.com/manual/有手册可以查询,Rob Allen在http://akrabat.com/zend-framework/介绍了一些他使用Zend Framework的经验,而Richard Thomas也在http://www.cyberlot.net/zendframenotes提供了一些有用的笔记。如果你有自己的想法,可以访问Zend Framework的新论坛:http://www.phparch.com/discuss/index.php/f/289//

 

结束语

 

要对预览版进行评价是很容易的事,我在写这个教程时也遇到很多困难。总的来说,我想Zend Framework显示了承诺,加入的每个人都是想继续完善它。

 

提示有什么评论、想法或问题,请访问Zend Framework的新论坛:http://www.phparch.com/discuss/index.php/f/289//

 

关于作者—Chris Shiflett是Brain Bulb的负责人,而Brain Bulb是一家专业的PHP开发和安全方面的顾问公司。Chris的blog是http://shiflett.org
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics