如何在zend freamwork 2中使用Flash信使?會話文檔還沒有。任何人都知道嗎?但會話庫在那裏。zf2中的Flash信使
回答
我前段時間寫了一篇文章。你可以找到它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; ?>
如果您需要進一步的說明,請參見我的博客,但我想代碼本身是相當清楚的(除了frmo佈局。 phtml例子)。或者,您總是可以自由編寫自己的視圖幫助器,使其在視圖模板中看起來更清晰。
如何在View Helper中抓取Flashmessenger的消息 - 按照Sam的要求共享代碼。
視圖幫助器應該實現ServiceManagerAwareInterface接口和相關方法。該插件現在可以訪問服務管理器,我們可以使用該服務器來獲取服務定位器並最終訪問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()從插件中應返回可以被傳遞到一個局部的和呈現消息數組。
更新:
Zend Framework的新版本中增加了FlashMessenger視圖助手,在路徑中找到/library/Zend/View/Helper/FlashMessenger.php
老答案:
我已經寫了定製vie w ^幫手,打印提示信息
在/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或您的視圖進行簡單調用。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>
希望這將有助於!下面的圖
代碼添加到渲染錯誤信息:
<?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...');
- 1. Windows Mobile中的Flash短信
- 2. 通信JavaScript和Flash
- 3. ZF2中的ViewModel
- 4. 在Zf2中使用Codeigniter
- 5. 使用php,mysql和flash跟蹤信息
- 6. ZF2 SessionManager使用
- 7. ZF2,ZF2-NoCaptcha,FormValidation
- 8. ZF2中的Zend_Db_Table_Abstract :: createRow()
- 9. TreeRouteStack ZF2中的段
- 10. ZF2中的會話
- 11. 在Flash中顯示視頻信息
- 12. 如何在ZF2中使用Yelp的API?
- 13. Flash和JavaScript通信問題
- 14. Flash/Javascript通信錯誤
- 15. 沒有Flash服務器的RTMFP通信
- 16. 的Javascript到Flash通信不起作用
- 17. Flash和flex之間的通信4
- 18. 使用Amazon S3服務和Flash AS3的域之間的通信
- 19. 在ControllerPlugin類中的ZF2 getServiceLocator
- 20. zf2工具類中的getServiceLocator
- 21. ZF2在subforlder中的應用
- 22. ZF2中的依賴注入
- 23. 在ZF2中的Ajax分頁?
- 24. ZF2中的SELECT(COUNT(*))查詢
- 25. zf2中的SEO友好URL
- 26. 註冊庫中ZF2
- 27. 在ZF2項目中使用ZF1模型
- 28. 在ZF2中使用MySQL持久連接
- 29. 無法在ZF2中使用控制檯
- 30. 如何在ZF2中使用BjyProfiler?
我發現它非常方便創建一個可以訪問和顯示消息的View助手。對於那些感興趣的人來說,可以通過實現ServiceManagerAwareInterface來完成,然後從ControllerPluginManager中獲取可以通過ServiceLocator訪問的FlashMessenger。這消除了每次將消息從操作傳遞到視圖模型的需要。 – DrBeza
如果你現在有,分享你的代碼如何? :)我相信這會幫助很多不熟悉它的人。我只列出了「魔術師」助手的基本工作;) – Sam