ホームページ  >  記事  >  バックエンド開発  >  Zend Framework アクション アシスタント FlashMessenger の使用状況分析

Zend Framework アクション アシスタント FlashMessenger の使用状況分析

不言
不言オリジナル
2018-06-19 11:48:071600ブラウズ

この記事では、主に Zend Framework のアクション アシスタント FlashMessenger の使用法を紹介し、アクション アシスタント FlashMessenger の機能を分析し、FlashMessenger の使用スキルを例の形式で示します。

この記事の例は Zend Framework のアクション アシスタント FlashMessenger の使用方法を学習しました。参考までに皆さんと共有してください。詳細は次のとおりです。

FlashMessenger は Flash Messenger セッションを処理するために使用されます。FlashMessenger は魔法のアシスタントです。

ユーザーが正常に登録した後、ユーザーの名前がプロンプト ページに表示される必要があります。リクエストが get を介して渡されない場合は、もちろんセッションを介してリクエストを渡すこともできます。

ユーザー名を表示します。ただし、session の操作は必然的に複雑になります。Flash Messenger を使用すると、この要件を迅速に実現できます。

FlashMessenger Assistant を使用すると、ユーザーが次のリクエストで 表示する必要があるメッセージを配信できます。

FlashMessenger は Zend_Session_Namespace を使用して、将来または次の読み取りリクエストに備えてメッセージを保存しますが、これは比較的単純です。

FlashMessenger の簡単な使用法:

helper_demo1 プロジェクトに基づく

/helper_demo1/application/controllers/UserController.php

#
<?php
class UserController extends Zend_Controller_Action
{
  protected $_flashMessenger = null;
  public function init()
  {
    $this->_flashMessenger =
    $this->_helper->getHelper(&#39;FlashMessenger&#39;);
    $this->initView();
  }
  public function registerAction()
  {
    $this->_flashMessenger->addMessage(&#39;xxxxx,Welcome!&#39;);
    $this->_helper->redirector(&#39;regtips&#39;);
  }
  public function regtipsAction()
  {
    $this->view->messages = $this->_flashMessenger->getMessages();
  }
}

New/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

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 &#39;Zend/Session.php&#39;;
/**
 * @see Zend_Controller_Action_Helper_Abstract
 */
require_once &#39;Zend/Controller/Action/Helper/Abstract.php&#39;;
/**
 * 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 &#39;default&#39;
   *
   * @var string
   */
  protected $_namespace = &#39;default&#39;;
  /**
   * __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 &#39;clean&#39; (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 = &#39;default&#39;)
  {
    $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);
  }
}

# 以上がこの記事の全内容です。その他の関連コンテンツについては、こちらをご覧ください。 PHP中国語ネットに注目!

関連する推奨事項:

Zend Framework での Zend_Config コンポーネントの使用法について


Zend Framework アクション アシスタントの使用法についてURL

Zend Framework での Zend_Db_Table_Rowset の使用法について


以上がZend Framework アクション アシスタント FlashMessenger の使用状況分析の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。