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 相关文章推荐
PHP调用三种数据库的方法(3)
Oct 09 PHP
将数字格式的计算结果转为汉字格式
Oct 09 PHP
mysql中存储过程、函数的一些问题
Feb 14 PHP
php中批量修改文件后缀名的函数代码
Oct 23 PHP
解析PHP高效率写法(详解原因)
Jun 20 PHP
浅析php中jsonp的跨域实例
Jun 21 PHP
ThinkPHP中I(),U(),$this-&gt;post()等函数用法
Nov 22 PHP
经典PHP加密解密函数Authcode()修复版代码
Apr 05 PHP
利用PHP命令行模式采集股票趋势信息
Aug 09 PHP
PHP未登录自动跳转到登录页面
Dec 21 PHP
ThinkPHP中调用PHPExcel的实现代码
Apr 08 PHP
PHP两个n位的二进制整数相加问题的解决
Aug 26 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
Linux下CoreSeek及PHP扩展模块的安装
2012/09/23 PHP
php创建多级目录与级联删除文件的方法示例
2019/09/12 PHP
JavaScript国旗变换效果代码
2008/08/13 Javascript
jquery命令汇总,方便使用jquery的朋友
2012/06/26 Javascript
JS常见问题整理(持续更新)
2013/08/06 Javascript
将HTML的左右尖括号等转义成实体形式的两种实现方式
2014/05/04 Javascript
BootStrap Tooltip插件源码解析
2016/12/27 Javascript
jQuery简单实现遍历单选框的方法
2017/03/06 Javascript
利用PM2部署node.js项目的方法教程
2017/05/10 Javascript
JavaScript实现设置默认日期范围为最近40天的方法分析
2017/07/12 Javascript
Django+Vue跨域环境配置详解
2018/07/06 Javascript
Vue数据绑定简析小结
2019/05/07 Javascript
自定义javascript验证框架示例【附源码下载】
2019/05/31 Javascript
详解Vue.js 作用域、slot用法(单个slot、具名slot)
2019/10/15 Javascript
基于canvas实现手写签名(vue)
2020/05/21 Javascript
vue实践---vue不依赖外部资源实现简单多语操作
2020/09/21 Javascript
vant自定义二级菜单操作
2020/11/02 Javascript
Nuxt 项目性能优化调研分析
2020/11/07 Javascript
[13:18]《一刀刀一天》之DOTA全时刻21:详解TI新赛制 A队再露獠牙
2014/06/24 DOTA
[48:23]DOTA2上海特级锦标赛主赛事日 - 4 败者组第四轮#1COL VS EG第一局
2016/03/05 DOTA
利用python获得时间的实例说明
2013/03/25 Python
python实现对求解最长回文子串的动态规划算法
2018/06/02 Python
Python3中内置类型bytes和str用法及byte和string之间各种编码转换 问题
2018/09/27 Python
Python实现滑动平均(Moving Average)的例子
2019/08/24 Python
基于python实现上传文件到OSS代码实例
2020/05/09 Python
C++和python实现阿姆斯特朗数字查找实例代码
2020/12/07 Python
莫斯科高科技在线商店:KremlinStore
2019/03/13 全球购物
造型师求职自荐信
2013/09/27 职场文书
简历自我评价怎么写呢?
2014/01/06 职场文书
小学英语教学反思
2014/01/30 职场文书
投标担保书范文
2014/04/02 职场文书
材料化学专业求职信
2014/07/15 职场文书
学校教师安全责任书
2014/07/23 职场文书
领导班子在批评与自我批评座谈会上的发言
2014/09/28 职场文书
2015年语言文字工作总结
2015/07/23 职场文书
使用Python脚本对GiteePages进行一键部署的使用说明
2021/05/27 Python