热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ZendFramework指南(4)

因为用户在提交了一个表单之后被重定向了,所以在这个控制器里面不需要视图。在AdminController.php中,要处理两个动作,显示管理界面和审批新闻:
下面,在视图目录中创建一些简单的模版。index.php文件可以用于显示index视图:
清单30 –

<html>
<
head>
  <
title>Newstitle>
head>
<
body>
  <
h1>Newsh1>
  php foreach ($this->news as $entry) { ?>
  


    echo $this->escape($entry{{'id'}}); ?>">
    echo $this->escape($entry{{'title'}}); ?>
    
  


  ?>
  Add News
  
  

Title:


  

Content:


  


  

 


view.php 模版可以用于显示特定的新闻条目:
清单31

<html>
<
head>
  <
title>
    php echo $this->escape($this->news{{'title'}}); ?>
  


  
    echo $this->escape($this->news{{'title'}}); ?>
  
  


    echo $this->escape($this->news{{'content'}}); ?>
  


  Comments
  foreach ($this->comments as $comment) { ?>
  


    echo $this->escape($comment{{'name'}}); ?> writes:
  


  

    echo $this->escape($comment{{'comment'}}); ?>
  

  ?>
  Add a Comment
  
       value="echo $this->escape($this->id); ?>" />
  

Name:


  

Comment:


  


  

 


最后,admin.php模版可以用于审批新闻条目:
清单32
<html>
<
head>
  <
title>News Admintitle>
head>
<
body>
  <
form action="/admin/approve" method="POST">
  php foreach ($this->news as $entry) { ?>
  


         value="echo $this->escape($entry{{'id'}}); ?>" />
    echo $this->escape($entry{{'title'}}); ?>
    echo $this->escape($entry{{'content'}}); ?>
  


  ?>
  


    Password:
  


  


  

 

为了简单起见,就使用一个带密码的表单作为访问控制机制。
放好了这些模版之后,就只要将原来放在控制器中占着位置的注释替换成几行代码。例如,
IndexController.php就变成了:
清单33

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还要再
处理一下,需要更多的代码:
清单34

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('/');
    }
}

?> 


因为用户在提交了一个表单之后被重定向了,所以在这个控制器里面不需要视图。
在AdminController.php中,要处理两个动作,显示管理界面和审批新闻:
清单35

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('/');
    }
}

?> 


推荐阅读
author-avatar
zhouib8oevlap
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有