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 相关文章推荐
怎么使 Mysql 数据同步
Oct 09 PHP
利用文件属性结合Session实现在线人数统计
Oct 09 PHP
PHP字符过滤函数去除字符串最后一个逗号(rtrim)
Mar 26 PHP
在windows平台上构建自己的PHP实现方法(仅适用于php5.2)
Jul 05 PHP
php上传文件中文文件名乱码的解决方法
Nov 01 PHP
Smarty中的注释和截断功能介绍
Apr 09 PHP
CI(Codeigniter)的Setting增强配置类实例
Jan 06 PHP
php获取文件名称和扩展名的方法
Feb 07 PHP
ajax调用返回php接口返回json数据的方法(必看篇)
May 05 PHP
PHP实现基于图的深度优先遍历输出1,2,3...n的全排列功能
Nov 10 PHP
PHP分享图片的生成方法
Apr 25 PHP
浅谈laravel数据库查询返回的数据形式
Oct 21 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
优化NFR之一 --MSSQL Hello Buffer Overflow
2006/10/09 PHP
php的ddos攻击解决方法
2015/01/08 PHP
php+ajax无刷新上传图片实例代码
2015/11/17 PHP
读jQuery之十三 添加事件和删除事件的核心方法
2011/08/23 Javascript
关于URL中的特殊符号使用介绍
2011/11/03 Javascript
解决jquery异步按一定的时间间隔刷新问题
2012/12/10 Javascript
jQuery选择器简明总结(含用法实例,一目了然)
2014/04/25 Javascript
全面解析bootstrap格子布局
2016/05/22 Javascript
jquery的ajax提交form表单的两种方法小结(推荐)
2016/05/25 Javascript
JavaScript中实现键值对应的字典与哈希表结构的示例
2016/06/12 Javascript
IONIC自定义subheader的最佳解决方案
2016/09/22 Javascript
JS自定义函数对web前端上传的文件进行类型大小判断
2016/10/19 Javascript
WEB开发之注册页面验证码倒计时代码的实现
2016/12/15 Javascript
谈谈第三方App接入微信登录 解读
2016/12/27 Javascript
微信小程序学习(4)-系统配置app.json详解
2017/01/12 Javascript
在VUE style中使用data中的变量的方法
2020/06/19 Javascript
Vue按时间段查询数据组件使用详解
2020/08/21 Javascript
[03:09]DOTA2亚洲邀请赛 LGD战队出场宣传片
2015/02/07 DOTA
Google开源的Python格式化工具YAPF的安装和使用教程
2016/05/31 Python
Python中线程的MQ消息队列实现以及消息队列的优点解析
2016/06/29 Python
python多线程之事件Event的使用详解
2018/04/27 Python
用python建立两个Y轴的XY曲线图方法
2019/07/08 Python
python提取log文件内容并画出图表
2019/07/08 Python
Python实现时间序列可视化的方法
2019/08/06 Python
Python tkinter模版代码实例
2020/02/05 Python
Python-jenkins 获取job构建信息方式
2020/05/12 Python
Python实现电视里的5毛特效实例代码详解
2020/05/15 Python
经验丰富程序员才知道的8种高级Python技巧
2020/07/27 Python
CSS3 2D模拟实现摩天轮旋转效果
2016/11/16 HTML / CSS
全球最大的瓷器、水晶和银器零售商:Replacements
2020/06/15 全球购物
触发器(trigger)的功能都有哪些?写出一个触发器的例子
2012/09/17 面试题
大学生应聘自荐信
2013/10/11 职场文书
自荐信怎么写好
2013/11/11 职场文书
学习焦裕禄同志为人民服务思想汇报
2014/09/10 职场文书
go语言求任意类型切片的长度操作
2021/04/26 Golang
mysql性能优化以及配置连接参数设置
2022/05/06 MySQL