找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1348|回复: 0
打印 上一主题 下一主题

Zend Framework动作助手FlashMessenger用法详解

[复制链接]

2617

主题

2617

帖子

7789

积分

论坛元老

Rank: 8Rank: 8

积分
7789
跳转到指定楼层
楼主
发表于 2018-2-14 05:38:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            本文实例讲述了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
_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
test

messages);
?>

访问http://www.localzend.com/helper_demo1/public/user/register
跳转到http://www.localzend.com/helper_demo1/public/user/regtips
FlashMessager实现源码如下
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程序设计有所帮助。
            
            
您可能感兴趣的文章:
  • Zend Framework动作助手(Zend_Controller_Action_Helper)用法详解
  • Zend Framework创建自己的动作助手详解
  • Zend Framework动作助手Json用法实例分析
  • Zend Framework动作助手Url用法详解
  • Zend Framework动作助手Redirector用法实例详解
  • Zend Framework教程之前端控制器Zend_Controller_Front用法详解
  • Zend Framework教程之动作的基类Zend_Controller_Action详解
  • Zend Framework开发入门经典教程
  • Zend Framework教程之Zend_Helpers动作助手ViewRenderer用法详解
  • php入门教程之Zend Studio设置与开发实例
  • Zend Framework动作控制器用法示例
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端