当前位置:首页 > CMS教程 > 其它CMS > 列表

Zend Framework动作助手FlashMessenger用法详解

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-13 14:41:09 浏览: 评论:0 

这篇文章主要介绍了Zend Framework动作助手FlashMessenger用法,分析了动作助手FlashMessenger的功能,并结合实例形式演示了FlashMessenger的使用技巧,需要的朋友可以参考下。

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

  1. <?php 
  2. class UserController extends Zend_Controller_Action 
  3. { 
  4.   protected $_flashMessenger = null; 
  5.   public function init() 
  6.   { 
  7.     $this->_flashMessenger = 
  8.     $this->_helper->getHelper('FlashMessenger'); 
  9.     $this->initView(); 
  10.   } 
  11.   public function registerAction() 
  12.   { 
  13.     $this->_flashMessenger->addMessage('xxxxx,Welcome!'); 
  14.     $this->_helper->redirector('regtips'); 
  15.   } 
  16.   public function regtipsAction() 
  17.   { 
  18.     $this->view->messages = $this->_flashMessenger->getMessages(); 
  19.   } 
  20. } 

新增/helper_demo1/application/views/scripts/user/regtips.phtml

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  2. "http://www.w3.org/TR/html4/loose.dtd"> 
  3. <html> 
  4. <head> 
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  6. <title>test</title> 
  7. </head> 
  8. <body> 
  9. <?php 
  10. var_dump($this->messages); 
  11. ?> 
  12. </body> 
  13. </html> 

访问http://www.localzend.com/helper_demo1/public/user/register

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

FlashMessager实现源码如下

  1. <?php 
  2. /** 
  3.  * Zend Framework 
  4.  * 
  5.  * LICENSE 
  6.  * 
  7.  * This source file is subject to the new BSD license that is bundled 
  8.  * with this package in the file LICENSE.txt. 
  9.  * It is also available through the world-wide-web at this URL: 
  10.  * http://framework.zend.com/license/new-bsd 
  11.  * If you did not receive a copy of the license and are unable to 
  12.  * obtain it through the world-wide-web, please send an email 
  13.  * to license@zend.com so we can send you a copy immediately. 
  14.  * 
  15.  * @category  Zend 
  16.  * @package  Zend_Controller 
  17.  * @subpackage Zend_Controller_Action_Helper 
  18.  * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) 
  19.  * @license  http://framework.zend.com/license/new-bsd   New BSD License 
  20.  */ 
  21. /** 
  22.  * @see Zend_Session 
  23.  */ 
  24. require_once 'Zend/Session.php'; 
  25. /** 
  26.  * @see Zend_Controller_Action_Helper_Abstract 
  27.  */ 
  28. require_once 'Zend/Controller/Action/Helper/Abstract.php'; 
  29. /** 
  30.  * Flash Messenger - implement session-based messages 
  31.  * 
  32.  * @uses    Zend_Controller_Action_Helper_Abstract 
  33.  * @category  Zend 
  34.  * @package  Zend_Controller 
  35.  * @subpackage Zend_Controller_Action_Helper 
  36.  * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) 
  37.  * @license  http://framework.zend.com/license/new-bsd   New BSD License 
  38.  * @version  $Id: FlashMessenger.php 23775 2011-03-01 17:25:24Z ralph $ 
  39.  */ 
  40. class Zend_Controller_Action_Helper_FlashMessenger extends Zend_Controller_Action_Helper_Abstract implements IteratorAggregate, Countable 
  41. { 
  42.   /** 
  43.    * $_messages - Messages from previous request 
  44.    * 
  45.    * @var array 
  46.    */ 
  47.   static protected $_messages = array(); 
  48.   /** 
  49.    * $_session - Zend_Session storage object 
  50.    * 
  51.    * @var Zend_Session 
  52.    */ 
  53.   static protected $_session = null; 
  54.   /** 
  55.    * $_messageAdded - Wether a message has been previously added 
  56.    * 
  57.    * @var boolean 
  58.    */ 
  59.   static protected $_messageAdded = false; 
  60.   /** 
  61.    * $_namespace - Instance namespace, default is 'default' 
  62.    * 
  63.    * @var string 
  64.    */ 
  65.   protected $_namespace = 'default'; 
  66.   /** 
  67.    * __construct() - Instance constructor, needed to get iterators, etc 
  68.    * 
  69.    * @param string $namespace 
  70.    * @return void 
  71.    */ 
  72.   public function __construct() 
  73.   { 
  74.     if (!self::$_session instanceof Zend_Session_Namespace) { 
  75.       self::$_session = new Zend_Session_Namespace($this->getName()); 
  76.       foreach (self::$_session as $namespace => $messages) { 
  77.         self::$_messages[$namespace] = $messages; 
  78.         unset(self::$_session->{$namespace}); 
  79.       } 
  80.     } 
  81.   } 
  82.   /** 
  83.    * postDispatch() - runs after action is dispatched, in this 
  84.    * case, it is resetting the namespace in case we have forwarded to a different 
  85.    * action, Flashmessage will be 'clean' (default namespace) 
  86.    * 
  87.    * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface 
  88.    */ 
  89.   public function postDispatch() 
  90.   { 
  91.     $this->resetNamespace(); 
  92.     return $this; 
  93.   } 
  94.   /** 
  95.    * setNamespace() - change the namespace messages are added to, useful for 
  96.    * per action controller messaging between requests 
  97.    * 
  98.    * @param string $namespace 
  99.    * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface 
  100.    */ 
  101.   public function setNamespace($namespace = 'default') 
  102.   { 
  103.     $this->_namespace = $namespace; 
  104.     return $this; 
  105.   } 
  106.   /** 
  107.    * resetNamespace() - reset the namespace to the default 
  108.    * 
  109.    * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface 
  110.    */ 
  111.   public function resetNamespace() 
  112.   { 
  113.     $this->setNamespace(); 
  114.     return $this; 
  115.   } 
  116.   /** 
  117.    * addMessage() - Add a message to flash message 
  118.    * 
  119.    * @param string $message 
  120.    * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface 
  121.    */ 
  122.   public function addMessage($message) 
  123.   { 
  124.     if (self::$_messageAdded === false) { 
  125.       self::$_session->setExpirationHops(1, null, true); 
  126.     } 
  127.     if (!is_array(self::$_session->{$this->_namespace})) { 
  128.       self::$_session->{$this->_namespace} = array(); 
  129.     } 
  130.     self::$_session->{$this->_namespace}[] = $message; 
  131.     return $this; 
  132.   } 
  133.   /** 
  134.    * hasMessages() - Wether a specific namespace has messages 
  135.    * 
  136.    * @return boolean 
  137.    */ 
  138.   public function hasMessages() 
  139.   { 
  140.     return isset(self::$_messages[$this->_namespace]); 
  141.   } 
  142.   /** 
  143.    * getMessages() - Get messages from a specific namespace 
  144.    * 
  145.    * @return array 
  146.    */ 
  147.   public function getMessages() 
  148.   { 
  149.     if ($this->hasMessages()) { 
  150.       return self::$_messages[$this->_namespace]; 
  151.     } 
  152.     return array(); 
  153.   } 
  154.   /** 
  155.    * Clear all messages from the previous request & current namespace 
  156.    * 
  157.    * @return boolean True if messages were cleared, false if none existed 
  158.    */ 
  159.   public function clearMessages() 
  160.   { 
  161.     if ($this->hasMessages()) { 
  162.       unset(self::$_messages[$this->_namespace]); 
  163.       return true; 
  164.     } 
  165.     return false; 
  166.   } 
  167.   /** 
  168.    * hasCurrentMessages() - check to see if messages have been added to current 
  169.    * namespace within this request 
  170.    * 
  171.    * @return boolean 
  172.    */ 
  173.   public function hasCurrentMessages() 
  174.   { 
  175.     return isset(self::$_session->{$this->_namespace}); 
  176.   } 
  177.   /** 
  178.    * getCurrentMessages() - get messages that have been added to the current 
  179.    * namespace within this request 
  180.    * 
  181.    * @return array 
  182.    */ 
  183.   public function getCurrentMessages() 
  184.   { 
  185.     if ($this->hasCurrentMessages()) { 
  186.       return self::$_session->{$this->_namespace}; 
  187.     } 
  188.     return array(); 
  189.   } 
  190.   /** 
  191.    * clear messages from the current request & current namespace 
  192.    * 
  193.    * @return boolean 
  194.    */ 
  195.   public function clearCurrentMessages() 
  196.   { 
  197.     if ($this->hasCurrentMessages()) { 
  198.       unset(self::$_session->{$this->_namespace}); 
  199.       return true; 
  200.     } 
  201.     return false; 
  202.   } 
  203.   /** 
  204.    * getIterator() - complete the IteratorAggregate interface, for iterating 
  205.    * 
  206.    * @return ArrayObject 
  207.    */ 
  208.   public function getIterator() 
  209.   { 
  210.     if ($this->hasMessages()) { 
  211.       return new ArrayObject($this->getMessages()); 
  212.     } 
  213.     return new ArrayObject(); 
  214.   } 
  215.   /** 
  216.    * count() - Complete the countable interface 
  217.    * 
  218.    * @return int 
  219.    */ 
  220.   public function count() 
  221.   { 
  222.     if ($this->hasMessages()) { 
  223.       return count($this->getMessages()); 
  224.     } 
  225.     return 0; 
  226.   } 
  227.   /** 
  228.    * Strategy pattern: proxy to addMessage() 
  229.    * 
  230.    * @param string $message 
  231.    * @return void 
  232.    */ 
  233.   public function direct($message) 
  234.   { 
  235.     return $this->addMessage($message); 
  236.   } 
  237. }

Tags: Framework FlashMessenger

分享到: