当前位置: 移动技术网 > IT编程>开发语言>PHP > Zend Framework动作助手FlashMessenger用法详解

Zend Framework动作助手FlashMessenger用法详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

欧锦赛赛程表,ca3534,喜爱夜蒲 连诗雅

本文实例讲述了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程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网