我在Zend Framework 2.2.0中使用flashMesssenger將消息從一個動作傳遞到另一個動作。flashmessenger不顯示新消息
我有一個聯繫我們的表格,它被執行後,我發送一封電子郵件,並轉發用戶到主頁。
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 ?>
,但由於某種原因,我沒有得到任何的消息!它會正確地轉發用戶,但這就是它。
什麼想法?
可能是由於您使用的控制器的方式插件裏面的佈局導致問題..?最好在這種情況下創建一個View Helper .. – Andrew