Zend Framework动作助手FlashMessenger用法详解


Posted in PHP onMarch 05, 2016

本文实例讲述了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);
  }
}

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

PHP 相关文章推荐
在线短消息收发的程序,不用数据库
Oct 09 PHP
PHP经典的给图片加水印程序
Dec 06 PHP
PHP 和 MySQL 开发的 8 个技巧
Jan 02 PHP
PHP运行时强制显示出错信息的代码
Apr 20 PHP
基于PHP的cURL快速入门教程 (小偷采集程序)
Jun 02 PHP
PHP Undefined index报错的修复方法
Jul 17 PHP
非常好用的两个PHP函数 serialize()和unserialize()
Feb 04 PHP
ThinkPHP写第一个模块应用
Feb 20 PHP
PHP跨时区(UTC时间)应用解决方案
Jan 11 PHP
Yii中的relations数据关联查询及统计功能用法详解
Jul 14 PHP
ThinkPHP框架使用redirect实现页面重定向的方法实例分析
Apr 12 PHP
PHP PDOStatement::rowCount讲解
Feb 01 PHP
Zend Framework创建自己的动作助手详解
Mar 05 #PHP
Zend Framework动作助手(Zend_Controller_Action_Helper)用法详解
Mar 05 #PHP
Zend Framework实现Zend_View集成Smarty模板系统的方法
Mar 05 #PHP
Zend Framework教程之视图组件Zend_View用法详解
Mar 05 #PHP
Zend Framework教程之模型Model用法简单实例
Mar 04 #PHP
基于PHP实现等比压缩图片大小
Mar 04 #PHP
Zend Framework教程之模型Model基本规则和使用方法
Mar 04 #PHP
You might like
php通过数组实现多条件查询实现方法(字符串分割)
2014/05/06 PHP
使用prototype.js进行异步操作
2007/02/07 Javascript
javascript数组的扩展实现代码集合
2008/06/01 Javascript
jquery中eq和get的区别与使用方法
2011/04/14 Javascript
JavaScript中实现PHP的打乱数组函数shuffle实例
2014/10/11 Javascript
小米公司JavaScript面试题
2014/12/29 Javascript
javascript 实现map集合
2015/04/03 Javascript
日常收藏的jquery技巧
2015/12/02 Javascript
AngularJS ng-bind 指令简单实现
2016/07/30 Javascript
JavaScript中push(),join() 函数 实例详解
2016/09/06 Javascript
jquery attr()设置和获取属性值实例教程
2016/09/25 Javascript
jQuery将表单序列化成一个Object对象的实例
2016/11/29 Javascript
livereload工具实现前端可视化开发【推荐】
2016/12/23 Javascript
微信小程序开发探究
2016/12/27 Javascript
js中获取键盘按下键值event.keyCode、event.charCode和event.which的兼容性详解
2017/03/15 Javascript
JS非空验证及邮箱验证的实例
2017/08/11 Javascript
Vue中封装input组件的实例详解
2017/10/17 Javascript
使用Bootstrap + Vue.js实现表格的动态展示、新增和删除功能
2017/11/27 Javascript
jquery validate 实现动态增加/删除验证规则操作示例
2019/10/28 jQuery
JavaScript进阶(一)变量声明提升实例分析
2020/05/09 Javascript
Python读取图片EXIF信息类库介绍和使用实例
2014/07/10 Python
Python中将字典转换为列表的方法
2016/09/21 Python
通过shell+python实现企业微信预警
2019/03/07 Python
pyqt5中QThread在使用时出现重复emit的实例
2019/06/21 Python
Python for i in range ()用法详解
2020/09/18 Python
Python -m参数原理及使用方法解析
2020/08/21 Python
详解background属性的8个属性值(面试题)
2020/11/02 HTML / CSS
EJB包括(SessionBean,EntityBean)说出他们的生命周期,及如何管理事务的?
2013/02/17 面试题
物流管理专业职业生涯规划书
2014/01/06 职场文书
《鸿门宴》教学反思
2014/04/22 职场文书
小学生环保倡议书
2014/05/15 职场文书
幼儿园工作总结2015
2015/04/01 职场文书
材料员岗位职责范本
2015/04/11 职场文书
二手房购房意向书
2015/05/09 职场文书
亮剑观后感300字
2015/06/05 职场文书
css背景和边框标签实例详解
2021/05/21 HTML / CSS