2012-09-21 18 views

回答

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; ?> 

如果您需要進一步的說明,請參見我的博客,但我想代碼本身是相當清楚的(除了frmo佈局。 phtml例子)。或者,您總是可以自由編寫自己的視圖幫助器,使其在視圖模板中看起來更清晰。

+1

我發現它非常方便創建一個可以訪問和顯示消息的View助手。對於那些感興趣的人來說,可以通過實現ServiceManagerAwareInterface來完成,然後從ControllerPluginManager中獲取可以通過ServiceLocator訪問的FlashMessenger。這消除了每次將消息從操作傳遞到視圖模型的需要。 – DrBeza

+2

如果你現在有,分享你的代碼如何? :)我相信這會幫助很多不熟悉它的人。我只列出了「魔術師」助手的基本工作;) – Sam

3

如何在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()從插件中應返回可以被傳遞到一個局部的和呈現消息數組。

15

更新:

Zend Framework的新版本中增加了FlashMessenger視圖助手,在路徑中找到/library/Zend/View/Helper/FlashMessenger.php

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> 

希望這將有助於!下面的圖

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...');