Zend Framework动作助手FlashMessenger用法详解

 更新时间:2016年03月05日 10:04:01   作者:coder  
这篇文章主要介绍了Zend Framework动作助手FlashMessenger用法,分析了动作助手FlashMessenger的功能,并结合实例形式演示了FlashMessenger的使用技巧,需要的朋友可以参考下

本文实例讲述了Zend Framework动作助手FlashMessenger用法。分享给大家供大家参考,具体如下:

FlashMessenger 用来处理Flash Messenger会话;FlashMessenger是一个神奇的助手。

有这么一种场景,在用户注册成功后,需要在提示页面上显示用户的名称,如果不通过get传递请求,当然你也可以通过session传递

要显示的用户名称。但是seesion的操作难免复杂,可以使用Flash Messenger快速的实现这个需求。

FlashMessenger助手允许你传递用户可能需要在下个请求看到的消息

FlashMessenger也是使用Zend_Session_Namespace来存储消息以备将来或下个请求来读取,但是相对简单一些

FlashMessenger简单用法

在helper_demo1项目的基础上

新增/helper_demo1/application/controllers/UserController.php

<?php
class UserController extends Zend_Controller_Action
{
  protected $_flashMessenger = null;
  public function init()
  {
    $this->_flashMessenger =
    $this->_helper->getHelper('FlashMessenger');
    $this->initView();
  }
  public function registerAction()
  {
    $this->_flashMessenger->addMessage('xxxxx,Welcome!');
    $this->_helper->redirector('regtips');
  }
  public function regtipsAction()
  {
    $this->view->messages = $this->_flashMessenger->getMessages();
  }
}

新增/helper_demo1/application/views/scripts/user/regtips.phtml

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>
<?php
var_dump($this->messages);
?>
</body>
</html>

访问http://www.localzend.com/helper_demo1/public/user/register
跳转到http://www.localzend.com/helper_demo1/public/user/regtips

FlashMessager实现源码如下

<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category  Zend
 * @package  Zend_Controller
 * @subpackage Zend_Controller_Action_Helper
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license  http://framework.zend.com/license/new-bsd   New BSD License
 */
/**
 * @see Zend_Session
 */
require_once 'Zend/Session.php';
/**
 * @see Zend_Controller_Action_Helper_Abstract
 */
require_once 'Zend/Controller/Action/Helper/Abstract.php';
/**
 * Flash Messenger - implement session-based messages
 *
 * @uses    Zend_Controller_Action_Helper_Abstract
 * @category  Zend
 * @package  Zend_Controller
 * @subpackage Zend_Controller_Action_Helper
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license  http://framework.zend.com/license/new-bsd   New BSD License
 * @version  $Id: FlashMessenger.php 23775 2011-03-01 17:25:24Z ralph $
 */
class Zend_Controller_Action_Helper_FlashMessenger extends Zend_Controller_Action_Helper_Abstract implements IteratorAggregate, Countable
{
  /**
   * $_messages - Messages from previous request
   *
   * @var array
   */
  static protected $_messages = array();
  /**
   * $_session - Zend_Session storage object
   *
   * @var Zend_Session
   */
  static protected $_session = null;
  /**
   * $_messageAdded - Wether a message has been previously added
   *
   * @var boolean
   */
  static protected $_messageAdded = false;
  /**
   * $_namespace - Instance namespace, default is 'default'
   *
   * @var string
   */
  protected $_namespace = 'default';
  /**
   * __construct() - Instance constructor, needed to get iterators, etc
   *
   * @param string $namespace
   * @return void
   */
  public function __construct()
  {
    if (!self::$_session instanceof Zend_Session_Namespace) {
      self::$_session = new Zend_Session_Namespace($this->getName());
      foreach (self::$_session as $namespace => $messages) {
        self::$_messages[$namespace] = $messages;
        unset(self::$_session->{$namespace});
      }
    }
  }
  /**
   * postDispatch() - runs after action is dispatched, in this
   * case, it is resetting the namespace in case we have forwarded to a different
   * action, Flashmessage will be 'clean' (default namespace)
   *
   * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
   */
  public function postDispatch()
  {
    $this->resetNamespace();
    return $this;
  }
  /**
   * setNamespace() - change the namespace messages are added to, useful for
   * per action controller messaging between requests
   *
   * @param string $namespace
   * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
   */
  public function setNamespace($namespace = 'default')
  {
    $this->_namespace = $namespace;
    return $this;
  }
  /**
   * resetNamespace() - reset the namespace to the default
   *
   * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
   */
  public function resetNamespace()
  {
    $this->setNamespace();
    return $this;
  }
  /**
   * addMessage() - Add a message to flash message
   *
   * @param string $message
   * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
   */
  public function addMessage($message)
  {
    if (self::$_messageAdded === false) {
      self::$_session->setExpirationHops(1, null, true);
    }
    if (!is_array(self::$_session->{$this->_namespace})) {
      self::$_session->{$this->_namespace} = array();
    }
    self::$_session->{$this->_namespace}[] = $message;
    return $this;
  }
  /**
   * hasMessages() - Wether a specific namespace has messages
   *
   * @return boolean
   */
  public function hasMessages()
  {
    return isset(self::$_messages[$this->_namespace]);
  }
  /**
   * getMessages() - Get messages from a specific namespace
   *
   * @return array
   */
  public function getMessages()
  {
    if ($this->hasMessages()) {
      return self::$_messages[$this->_namespace];
    }
    return array();
  }
  /**
   * Clear all messages from the previous request & current namespace
   *
   * @return boolean True if messages were cleared, false if none existed
   */
  public function clearMessages()
  {
    if ($this->hasMessages()) {
      unset(self::$_messages[$this->_namespace]);
      return true;
    }
    return false;
  }
  /**
   * hasCurrentMessages() - check to see if messages have been added to current
   * namespace within this request
   *
   * @return boolean
   */
  public function hasCurrentMessages()
  {
    return isset(self::$_session->{$this->_namespace});
  }
  /**
   * getCurrentMessages() - get messages that have been added to the current
   * namespace within this request
   *
   * @return array
   */
  public function getCurrentMessages()
  {
    if ($this->hasCurrentMessages()) {
      return self::$_session->{$this->_namespace};
    }
    return array();
  }
  /**
   * clear messages from the current request & current namespace
   *
   * @return boolean
   */
  public function clearCurrentMessages()
  {
    if ($this->hasCurrentMessages()) {
      unset(self::$_session->{$this->_namespace});
      return true;
    }
    return false;
  }
  /**
   * getIterator() - complete the IteratorAggregate interface, for iterating
   *
   * @return ArrayObject
   */
  public function getIterator()
  {
    if ($this->hasMessages()) {
      return new ArrayObject($this->getMessages());
    }
    return new ArrayObject();
  }
  /**
   * count() - Complete the countable interface
   *
   * @return int
   */
  public function count()
  {
    if ($this->hasMessages()) {
      return count($this->getMessages());
    }
    return 0;
  }
  /**
   * Strategy pattern: proxy to addMessage()
   *
   * @param string $message
   * @return void
   */
  public function direct($message)
  {
    return $this->addMessage($message);
  }
}

更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

  • Yii隐藏URL中index.php的方法

    Yii隐藏URL中index.php的方法

    这篇文章主要介绍了Yii隐藏URL中index.php的方法,涉及Yii配置文件的设置与伪静态的配置技巧,需要的朋友可以参考下
    2016-07-07
  • CI框架安全类Security.php源码分析

    CI框架安全类Security.php源码分析

    之前我们分析了CI框架的session类session.php,本文我们继续分析CI框架的安全类security.php文件,方便我们更详细的了解CI框架,从而更熟练的应用CI框架
    2014-11-11
  • PHP DataGrid 实现代码

    PHP DataGrid 实现代码

    最近想做一个通过PHP实现DataGrid功能的东西,这样可以直接修改数据库中表的内容,而不用开发【新增数据页面】,【编辑页面】,于是乎在网上找了找,类似的东西也有几个,开源的、付费的都有,不过基本都是基于MySQL。
    2009-08-08
  • Zend Framework连接Mysql数据库实例分析

    Zend Framework连接Mysql数据库实例分析

    这篇文章主要介绍了Zend Framework连接Mysql数据库的方法,以完整实例形式分析了Zend Framework连接MySQL数据库的具体步骤与相关实现技巧,需要的朋友可以参考下
    2016-03-03
  • ThinkPHP中的create方法与自动令牌验证实例教程

    ThinkPHP中的create方法与自动令牌验证实例教程

    这篇文章主要介绍了ThinkPHP中的create方法与自动令牌验证的实现方法,有着非常重要的用途,需要的朋友可以参考下
    2014-08-08
  • 解决Laravel 使用insert插入数据,字段created_at为0000的问题

    解决Laravel 使用insert插入数据,字段created_at为0000的问题

    今天小编就为大家分享一篇解决Laravel 使用insert插入数据,字段created_at为0000的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-10-10
  • laravel实现上传图片,并且制作缩略图,按照日期存放的代码

    laravel实现上传图片,并且制作缩略图,按照日期存放的代码

    今天小编就为大家分享一篇laravel实现上传图片,并且制作缩略图,按照日期存放的代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-10-10
  • laravel-admin 中列表筛选方法

    laravel-admin 中列表筛选方法

    今天小编就为大家分享一篇laravel-admin 中列表筛选方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-10-10
  • CakePHP框架Session设置方法分析

    CakePHP框架Session设置方法分析

    这篇文章主要介绍了CakePHP框架Session设置方法,结合具体实例形式分析了CakePHP常见的Session操作技巧及相关注意事项,需要的朋友可以参考下
    2017-02-02
  • php设计模式之观察者模式定义与用法经典示例

    php设计模式之观察者模式定义与用法经典示例

    这篇文章主要介绍了php设计模式之观察者模式定义与用法,结合完整实例形式详细分析了php观察者模式概念、原理、定义与使用方法,代码注释包含详尽的说明,需要的朋友可以参考下
    2019-09-09

最新评论