最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Zend Framework動作助手FlashMessenger用法詳解

 更新時間:2016年03月05日 10:04:01   作者:coder  
這篇文章主要介紹了Zend Framework動作助手FlashMessenger用法,分析了動作助手FlashMessenger的功能,并結(jié)合實例形式演示了FlashMessenger的使用技巧,需要的朋友可以參考下

本文實例講述了Zend Framework動作助手FlashMessenger用法。分享給大家供大家參考,具體如下:

FlashMessenger 用來處理Flash Messenger會話;FlashMessenger是一個神奇的助手。

有這么一種場景,在用戶注冊成功后,需要在提示頁面上顯示用戶的名稱,如果不通過get傳遞請求,當(dāng)然你也可以通過session傳遞

要顯示的用戶名稱。但是seesion的操作難免復(fù)雜,可以使用Flash Messenger快速的實現(xiàn)這個需求。

FlashMessenger助手允許你傳遞用戶可能需要在下個請求看到的消息。

FlashMessenger也是使用Zend_Session_Namespace來存儲消息以備將來或下個請求來讀取,但是相對簡單一些

FlashMessenger簡單用法

在helper_demo1項目的基礎(chǔ)上

新增/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
跳轉(zhuǎn)到http://www.localzend.com/helper_demo1/public/user/regtips

FlashMessager實現(xiàn)源碼如下

<?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);
  }
}

更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《Yii框架入門及常用技巧總結(jié)》、《ThinkPHP入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

  • Laravel中錯誤與異常處理的用法示例

    Laravel中錯誤與異常處理的用法示例

    異常拋出在開發(fā)中錯誤處理是非常重要的,下面這篇文章主要給大家介紹了關(guān)于Laravel中錯誤與異常處理用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧
    2018-09-09
  • 如何修改Laravel中url()函數(shù)生成URL的根地址

    如何修改Laravel中url()函數(shù)生成URL的根地址

    這篇文章主要給大家介紹了關(guān)于如何修改Laravel中url()函數(shù)生成URL根地址的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用laravel具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • php實現(xiàn)信用卡校驗位算法THE LUHN MOD-10示例

    php實現(xiàn)信用卡校驗位算法THE LUHN MOD-10示例

    這篇文章主要介紹了php實現(xiàn)信用卡校驗位算法THE LUHN MOD-10的示例,需要的朋友可以參考下
    2014-05-05
  • Thinkphp5 微信公眾號token驗證不成功的原因及解決方法

    Thinkphp5 微信公眾號token驗證不成功的原因及解決方法

    下面小編就為大家?guī)硪黄猅hinkphp5 微信公眾號token驗證不成功的原因及解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Yii安裝與使用Excel擴展的方法

    Yii安裝與使用Excel擴展的方法

    這篇文章主要介紹了Yii安裝與使用Excel擴展的方法,簡單分析了Yii中Excel擴展的下載、安裝及相關(guān)使用技巧,需要的朋友可以參考下
    2016-07-07
  • php實現(xiàn)批量壓縮圖片文件大小的腳本

    php實現(xiàn)批量壓縮圖片文件大小的腳本

    這篇文章主要介紹了php實現(xiàn)把某個目錄下的圖片進行批量壓縮文件大小的腳本,單反拍的圖片,體積太大,所以要壓縮,需要的朋友可以參考下
    2014-07-07
  • yii實現(xiàn)model添加默認值的方法(2種方法)

    yii實現(xiàn)model添加默認值的方法(2種方法)

    這篇文章主要介紹了yii實現(xiàn)model添加默認值的方法,結(jié)合實例分析了在rules()方法及在beforeSave()方法中設(shè)定兩種實現(xiàn)技巧,需要的朋友可以參考下
    2016-01-01
  • 微信API接口大全

    微信API接口大全

    本文給大家介紹的是個人總結(jié)的一些微信API接口,包括微信支付、微信紅包、微信卡券、微信小店等,十分的全面,有需要的小伙伴可以參考下。
    2015-04-04
  • PHP把JPEG圖片轉(zhuǎn)換成Progressive JPEG的方法

    PHP把JPEG圖片轉(zhuǎn)換成Progressive JPEG的方法

    這篇文章主要介紹了PHP把JPEG圖片轉(zhuǎn)換成Progressive JPEG的方法,本文同時講解了Baseline JPEG和Progressive JPEG的區(qū)別和它們的優(yōu)劣,需要的朋友可以參考下
    2014-06-06
  • Thinkphp實現(xiàn)MySQL讀寫分離操作示例

    Thinkphp實現(xiàn)MySQL讀寫分離操作示例

    這篇文章主要介紹了Thinkphp實現(xiàn)MySQL讀寫分離的方法,需要的朋友可以參考下
    2014-06-06

最新評論

绵阳市| 灵宝市| 汉川市| 清水河县| 客服| 洞口县| 丰台区| 门头沟区| 文山县| 浪卡子县| 锦州市| 东台市| 白城市| 禹城市| 巫溪县| 金川县| 美姑县| 开平市| 潍坊市| 闵行区| 濉溪县| 清丰县| 新密市| 永定县| 郴州市| 尉氏县| 鸡东县| 青铜峡市| 光泽县| 两当县| 天柱县| 泸溪县| 漳平市| 朝阳市| 嵩明县| 明水县| 凉城县| 桃源县| 郑州市| 伊吾县| 通河县|