首頁 後端開發 php教程 Zend Framework动作助手FlashMessenger用法详解_php实例

Zend Framework动作助手FlashMessenger用法详解_php实例

Jun 07, 2016 pm 05:09 PM
framework zend

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

<&#63;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>
<&#63;php
var_dump($this->messages);
&#63;>
</body>
</html>

登入後複製

访问http://www.localzend.com/helper_demo1/public/user/register
跳转到http://www.localzend.com/helper_demo1/public/user/regtips

FlashMessager实现源码如下

<&#63;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程序设计有所帮助。

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Microsoft NET Framework 安裝問題 錯誤代碼 0x800c0006 修復 Microsoft NET Framework 安裝問題 錯誤代碼 0x800c0006 修復 May 05, 2023 pm 04:01 PM

.NETFramework4是開發人員和最終使用者在Windows上執行最新版本的應用程式所必需的。但是,在下載安裝.NETFramework4時,許多用戶抱怨安裝程式在中途停止,顯示以下錯誤訊息-「 .NETFramework4hasnotbeeninstalledbecauseDownloadfailedwitherrorcode0x800c0006 」。在您的裝置上安裝.NETFramework4時,如果您也在體驗它,那麼您就來對了地方

如何在 Windows 11/10 上使用 SetupDiag 識別 Windows 升級問題 如何在 Windows 11/10 上使用 SetupDiag 識別 Windows 升級問題 Apr 17, 2023 am 10:07 AM

每當您的Windows11或Windows10PC出現升級或更新問題時,您通常會看到一個錯誤代碼,指示故障背後的實際原因。但是,有時,升級或更新失敗可能不會顯示錯誤代碼,這時就會混淆。有了方便的錯誤代碼,您可以確切地知道問題出在哪裡,因此您可以嘗試修復。但是由於沒有出現錯誤代碼,因此識別問題並解決它變得極具挑戰性。這會佔用您大量時間來簡單地找出錯誤背後的原因。在這種情況下,您可以嘗試使用Microsoft提供的名為SetupDiag的專用工具,該工具可協助您輕鬆識別錯誤背後的真

SCNotification 已停止運作 [修復它的 5 個步驟] SCNotification 已停止運作 [修復它的 5 個步驟] May 17, 2023 pm 09:35 PM

身為Windows用戶,您很可能會在每次啟動電腦時遇到SCNotification已停止工作錯誤。 SCNotification.exe是一個微軟系統通知文件,由於權限錯誤和點網故障等原因,每次啟動PC時都會崩潰。此錯誤也以其問題事件名稱而聞名。因此,您可能不會將其視為SCNotification已停止工作,而是將其視為錯誤clr20r3。在本文中,我們將探討您需要採取的所有步驟來修復SCNotification已停止運作,以免它再次困擾您。什麼是SCNotification.e

Microsoft .NET Framework 4.5.2、4.6 和 4.6.1 將於 2022 年 4 月終止支持 Microsoft .NET Framework 4.5.2、4.6 和 4.6.1 將於 2022 年 4 月終止支持 Apr 17, 2023 pm 02:25 PM

已安裝Microsoft.NET版本4.5.2、4.6或4.6.1的MicrosoftWindows用戶如果希望Microsoft將來透過產品更新支援該框架,則必須安裝較新版本的Microsoft框架。據微軟稱,這三個框架都將在2022年4月26日停止支援。支援日期結束後,產品將不會收到「安全修復或技術支援」。大多數家庭設備透過Windows更新保持最新。這些設備已經安裝了較新版本的框架,例如.NETFramework4.8。未自動更新的設備可能

適用於 Windows 11 的KB5012643破壞了.NET Framework 3.5應用程式 適用於 Windows 11 的KB5012643破壞了.NET Framework 3.5應用程式 May 09, 2023 pm 01:07 PM

自從我們談論影響安裝KB5012643forWindows11的用戶的新安全模式錯誤以來已經過去了一周。這個討厭的問題並沒有出現在微軟在發布當天發布的已知問題清單中,因此讓所有人都感到驚訝。好吧,就在您認為情況不會變得更糟的時候,微軟為安裝此累積更新的用戶投下了另一顆炸彈。 Windows11Build22000.652導致更多問題因此,這家科技公司警告Windows11用戶,他們在啟動和使用某些.NETFramework3.5應用程式時可能會遇到問題。聽起來很熟悉?不過請不要驚

如何在Zend框架中使用ACL(Access Control List)進行權限控制 如何在Zend框架中使用ACL(Access Control List)進行權限控制 Jul 29, 2023 am 09:24 AM

如何在Zend框架中使用ACL(AccessControlList)進行權限控制導言:在一個Web應用程式中,權限控制是至關重要的功能。它可以確保使用者只能存取其有權存取的頁面和功能,並防止未經授權的存取。 Zend框架提供了一種方便的方法來實現權限控制,即使用ACL(AccessControlList)元件。本文將介紹如何在Zend框架中使用ACL

PHP實作框架:Zend Framework入門教程 PHP實作框架:Zend Framework入門教程 Jun 19, 2023 am 08:09 AM

PHP實作框架:ZendFramework入門教學ZendFramework是PHP開發的開源網站框架,目前由ZendTechnologies維護,ZendFramework採用了MVC設計模式,提供了一系列可重複使用的程式碼庫,服務於實作Web2.0應用程式和Web服務。 ZendFramework深受PHP開發者的歡迎與推崇,擁有廣泛

酷冷至尊攜手Framework推出創新迷你機殼套件,相容於筆電主機板 酷冷至尊攜手Framework推出創新迷你機殼套件,相容於筆電主機板 Dec 15, 2023 pm 05:35 PM

12月9日消息,最近,酷冷至尊在台北電腦展上的一次展示活動中,展示了與筆記本模組化方案提供商framework合作的迷你機箱套件,這個套件的獨特之處在於,它可以兼容併安裝來自framework筆記本的主機板。目前,這款產品已開始在市場上銷售,售價定為39美元,以當前匯率折合約279元。這款機箱套件的型號被命名為「frameWORKMAINBOARDCASE」。在設計方面,它體現了極致的緊湊與實用性,尺寸僅為297x133x15毫米。它的設計初衷是為了能夠無縫連接到framework筆記本的

See all articles