Zend Framework動作助手FlashMessenger用法詳解,zendflashmessenger_PHP教程

來源:互聯網
上載者:User

Zend Framework動作助手FlashMessenger用法詳解,zendflashmessenger


本文執行個體講述了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

<?phpclass 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

test<?phpvar_dump($this->messages);?>

訪問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程式設計有所協助。

您可能感興趣的文章:

  • Zend Framework教程之MVC架構的Controller用法分析
  • Zend Framework教程之路由功能Zend_Controller_Router詳解
  • Zend Framework教程之Zend_Controller_Plugin外掛程式用法詳解
  • Zend Framework教程之響應對象的封裝Zend_Controller_Response執行個體詳解
  • Zend Framework教程之請求對象的封裝Zend_Controller_Request執行個體詳解
  • Zend Framework教程之動作的基類Zend_Controller_Action詳解
  • Zend Framework教程之分發器Zend_Controller_Dispatcher用法詳解
  • Zend Framework教程之前端控制器Zend_Controller_Front用法詳解
  • Zend Framework動作助手Redirector用法執行個體詳解
  • Zend Framework動作助手Url用法詳解
  • Zend Framework動作助手Json用法執行個體分析
  • Zend Framework教程之Resource Autoloading用法執行個體

http://www.bkjia.com/PHPjc/1106900.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1106900.htmlTechArticleZend Framework動作助手FlashMessenger用法詳解,zendflashmessenger 本文執行個體講述了Zend Framework動作助手FlashMessenger用法。分享給大家供大家參考,具體...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.