Zend 2 中的 FlashMessenger [英] FlashMessenger in Zend 2
问题描述
我正在做 zend 2 学习.现在我结帐 flashMessenger 助手.似乎没有在 layout.php 中调用 flashMessenger 的记录方法.因为我想在位于布局的 div 中显示所有消息(错误或成功),所以我需要在那里调用 flashMessenger.我不想每次都通过控制器的动作发送消息,只想要动作只添加消息和布局显示它们.
I am doing zend 2 Study. Now I checkout flashMessenger helper. It seems there are no documented way to call flashMessenger at layout.php. because I want to show all messages (error or success) at div located at layout, I need to call flashMessenger there. I do not want to send messages everytime via controller's actions and just want action only add message and layout show them.
虽然我对自定义帮助程序/库持开放态度,但内置解决方案是最好的.(我在 zend 1 上也没有太多工作,所以我不知道即使使用 zend 1 是否也很容易.)
While I am open to custom helper/libs But builtin solution is the best. ( I do not much work on zend 1 also, So I did not know if it is easily possible with even zend 1. )
我查看了一个帖子 如何在我的布局文件中访问 flashmessenger,在 zend 框架中? 但它有针对 zend 1 的自定义解决方案.所以我在想 zend 1 和 1 中是否都没有内置解决方案2.
I checkout one post How do I access flashmessenger in my layout file, in zend framework? But it have custom solution for zend 1. So I am thinking if zend have no built in solution at all in both 1 & 2.
推荐答案
我刚刚编写了自己的简单视图助手:
i just wrote my own simple viewhelper:
<?php
namespace My\View\Helper;
use Zend\View\Helper\AbstractHelper;
class FlashMessenger extends AbstractHelper
{
protected static $_flashMessenger;
public function __invoke($namespace = 'default') {
if (!self::$_flashMessenger) {
self::$_flashMessenger = new \Zend\Mvc\Controller\Plugin\FlashMessenger;
}
return self::$_flashMessenger->setNamespace($namespace);
}
}
像这样使用:
<? if ($this->flashMessenger()->hasMessages()): ?>
<ul>
<? foreach ($this->flashMessenger()->getMessages() as $message): ?>
<li><?= $message></li>
<? endforeach ?>
</ul>
<? endif ?>
这篇关于Zend 2 中的 FlashMessenger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!