我在Zend Framework 2.2.0中使用flashMesssenger将消息从一个动作传递到另一个动作。
我有一个联系我们表格,在我执行后,我发送电子邮件并将用户转发到主页。
public function contactUsExecuteAction() {
$request = $this->getRequest();
if ($request->isPost()) {
$contactUs = new ContactUs();
$form = new ContactUsForm();
$form->setInputFilter($contactUs->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$contactUs->exchangeArray($form->getData());
$name = $contactUs->name;
$email = $contactUs->email;
$message = $contactUs->message;
$completeMessage='name:'.$name."\n".'email:'.$email."\n".$message;
$subject = 'myalcoholist contact us';
mail('<EMAIL_ADDRESSS>',$subject,$completeMessage);
$this->flashMessenger()->addMessage("thank you for contacting myalcoholist.");
return $this->forward()->dispatch('home');
}
}
}
现在我明白,转发只是转发页面,我应该只在下一次迭代中获取消息,除非我使用getCurrentMessages。所以这是我的layout.phtml
<?php
use Zend\Mvc\Controller\Plugin\FlashMessenger;
$messenger = $this->flashMessenger()->getPluginFlashMessenger();
foreach(array(
FlashMessenger::NAMESPACE_ERROR,
FlashMessenger::NAMESPACE_SUCCESS,
FlashMessenger::NAMESPACE_INFO,
FlashMessenger::NAMESPACE_DEFAULT)
as $namespace):
$messenger->setNamespace($namespace);
$userMsgs = array_merge($messenger->getCurrentMessages(), $messenger->getMessages());
$messenger->clearCurrentMessages();
foreach($userMsgs as $msg):
$msgText = $msg;
if (is_array($msg)){
$msgText = $msg['message'];
}
?>
<div class="alert alert-<?=$namespace?>">
<?=$msgText?>
</div>
<?php endforeach ?>
<?php endforeach ?>
但由于某种原因,我没有收到任何消息!它确实正确地转发了用户,但这就是它。
任何想法?
答案 0 :(得分:4)
1)
不要使用向前,不会有新的请求。
尝试使用重定向,这样您就会收到新的请求,并会发现等待您的消息,您不需要任何额外的代码来使其正常工作。
$this->redirect()->toRoute('routeName');
或
$this->redirect()->toUrl('http://google.com/');
<强> 2)强>
如果你被认为在这样的视图中使用它,那么你总是可以做一个简单的事情 查看助手:
namespace Application\View\Helper;
use Zend\Mvc\Controller\Plugin\FlashMessenger as ZendFlash;
use Zend\View\Helper\AbstractHelper;
class FlashMessenger extends AbstractHelper
{
/**
* @var \Zend\Mvc\Controller\Plugin\FlashMessenger
*/
protected $flashMessenger;
/**
* @var array
*/
protected $namespaces = array(
'default',
'error',
'success',
'info',
'warning'
);
/**
* Set the Controller plugin
*
* @param Zend\Mvc\Controller\Plugin\FlashMessenger
*/
public function setFlashMessenger($flashMessenger)
{
$this->flashMessenger = $flashMessenger;
return $this;
}
/**
* @return string
*/
public function __invoke()
{
$messageString = '';
foreach($this->namespaces as $ns) {
$this->flashMessenger->setNamespace($ns);
$messages = $this->flashMessenger->getMessages();
if($this->flashMessenger->hasCurrentMessages()) {
$messages += $this->flashMessenger->getCurrentMessages();
$this->flashMessenger->clearCurrentMessages();
}
if(count($messages) > 0) {
// Twitter bootstrap message box
$messageString .= sprintf(
'<div class="container-fluid">
<div class="alert alert-%s fade in">
<button data-dismiss="alert" class="close" type="button">×</button>
%s
</div>
</div>',
$ns,
implode('<br />', $messages)
);
}
}
return $messageString;
}
}
现在在服务管理器中注入Flash控制器插件:
public function getViewHelperConfig()
{
return array(
'factories' => array(
'flashMessenger' => function($sm) {
$flash = $sm->getServiceLocator()
->get('ControllerPluginManager')
->get('flashmessenger')
;
$messages = new \Application\View\Helper\FlashMessenger();
$messages->setFlashMessenger($flash);
return $messages;
},
现在你可以更好地使用视图中的视图助手了:
<强> example.phtml 强>
<?php echo $this->flashMessenger() ?>