如何在zend框架2中使用flash消息?会话文档还没有。谁知道呢?但会话库就在那里。
答案 0 :(得分:15)
更新:
Zend Framework新版本添加了FlashMessenger View Helper,可在路径/library/Zend/View/Helper/FlashMessenger.php
旧回答:
我已经编写了一个自定义视图助手,用于打印Flash消息
在/module/Application/Module.php
中public function getViewHelperConfig()
{
return array(
'factories' => array(
'flashMessage' => function($sm) {
$flashmessenger = $sm->getServiceLocator()
->get('ControllerPluginManager')
->get('flashmessenger');
$message = new \My\View\Helper\FlashMessages( ) ;
$message->setFlashMessenger( $flashmessenger );
return $message ;
}
),
);
}
在/library/My/View/Helper/FlashMessages.php中创建自定义视图助手
namespace My\View\Helper;
use Zend\View\Helper\AbstractHelper;
class FlashMessages extends AbstractHelper
{
protected $flashMessenger;
public function setFlashMessenger( $flashMessenger )
{
$this->flashMessenger = $flashMessenger ;
}
public function __invoke( )
{
$namespaces = array(
'error' ,'success',
'info','warning'
);
// messages as string
$messageString = '';
foreach ( $namespaces as $ns ) {
$this->flashMessenger->setNamespace( $ns );
$messages = array_merge(
$this->flashMessenger->getMessages(),
$this->flashMessenger->getCurrentMessages()
);
if ( ! $messages ) continue;
$messageString .= "<div class='$ns'>"
. implode( '<br />', $messages )
.'</div>';
}
return $messageString ;
}
}
然后从layout.phtml或您的view.phtml
进行简单调用echo $this->flashMessage();
让我举例说明控制器动作
public function testFlashAction()
{
//set flash message
$this->flashMessenger()->setNamespace('warning')
->addMessage('Mail sending failed!');
//set flash message
$this->flashMessenger()->setNamespace('success')
->addMessage('Data added successfully');
// redirect to home page
return $this->redirect()->toUrl('/');
}
在主页中,它会打印
<div class="success">Data added successfully</div>
<div class="warning">Mail sending failed!</div>
希望这会有所帮助!
答案 1 :(得分:9)
我前段时间写了一篇关于此事的帖子。你可以找到它right here
基本上你就像以前一样使用它。
<?php
public function commentAction()
{
// ... display Form
// ... validate the Form
if ($form->isValid()) {
// try-catch passing data to database
$this->flashMessenger()->addMessage('Thank you for your comment!');
return $this->redirect()->toRoute('blog-details'); //id, blabla
}
}
public function detailsAction()
{
// Grab the Blog with given ID
// Grab all Comments for this blog
// Assign the view Variables
return array(
'blog' => $blog,
'comments' => $comments,
'flashMessages' => $this->flashMessenger()->getMessages()
);
}
然后在.phtml文件中,你这样做:
// details.phtml
<?php if(count($flashMessages)) : ?>
<ul>
<?php foreach ($flashMessages as $msg) : ?>
<li><?php echo $msg; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
显然这不是太方便,因为你必须为每个.phtml文件执行此操作。因此,在布局中执行此操作时,您最多只需执行以下操作:
<?php
// layout.phtml
// First get the viewmodel and all its children (ie the actions viewmodel)
$children = $this->viewModel()
->getCurrent()
->getChildren();
$ourView = $children[0];
if (isset($ourView->flashMessages) && count($ourView->flashMessages)) : ?>
<ul class="flashMessages">
<?php foreach ($ourView->flashMessages as $fMessage) : ?>
<li><?php echo $fMessage; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
如果您需要进一步的描述,请参阅我的博客,但我想代码本身非常清楚(除了layout.phtml示例)。或者,你总是可以自由编写自己的视图助手,让它在视图模板中看起来更清晰。
答案 2 :(得分:3)
如何在View Helper中抓取Flashmessenger的消息 - 按照Sam的要求共享代码。
View助手应该实现ServiceManagerAwareInterface接口和相关方法。该插件现在可以访问Service Manager,我们可以使用它来获取服务定位器并最终访问Flash Messenger。
自从我最初编写代码以来,我没有触及过这段代码 - 所以可能有一种更优雅的方式来做这件事。
protected function getMessages()
{
$serviceLocator = $this->getServiceManager()->getServiceLocator();
$plugin = $serviceLocator->get('ControllerPluginManager');
$flashMessenger = $plugin->get('flashmessenger');
$messages = $flashMessenger->getMessages();
// Check for any recently added messages
if ($flashMessenger->hasCurrentMessages())
{
$messages += $flashMessenger->getCurrentMessages();
$flashMessenger->clearCurrentMessages();
}
return $messages;
}
从插件中调用getMessages()应返回一个消息数组,这些消息可以传递给部分并呈现。
答案 3 :(得分:0)
在视图中添加以下代码以呈现错误消息:
<?php echo $this->flashmessenger()
->setMessageOpenFormat('<div class="alert alert-danger"><ul%s><li>')
->setMessageCloseString('</li></ul></div>')
->render('error')
; ?>
在上一个请求中,请确保通过在控制器中运行以下代码来创建错误消息:
$this->flashmessenger()->addErrorMessage('Whops, something went wrong...');