douhan9619 2016-02-22 16:19
浏览 53

在FlashMessenger中调用数组上的成员函数toArray()

I'm getting following error in my ZF2 FlashMessenger:

Call to a member function toArray() on array in /vendor/zendframework/zend-mvc/src/Controller/Plugin/FlashMessenger.php on line 306

FlashMessages will be rendered by my FlashMessageHelper:

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;
use Zend\View\Helper\FlashMessenger;

class FlashMessageHelper extends AbstractHelper {

  public function __invoke(FlashMessenger $flashMessenger) {

    $flashMessenger->setMessageOpenFormat('<div%s><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><p class="text-center">');

    $flashMessenger->setMessageSeparatorString('</p><p class="text-center">');

    $flashMessenger->setMessageCloseString('</p></div>');

    $flashMessenger->setAutoEscape(false);

    echo $flashMessenger->render('error', array(
        'alert',
        'alert-dismissible',
        'alert-danger'
    ));
    echo $flashMessenger->render('info', array(
        'alert',
        'alert-dismissible',
        'alert-info'
    ));
    echo $flashMessenger->render('default', array(
        'alert',
        'alert-dismissible',
        'alert-warning'
    ));
    echo $flashMessenger->render('success', array(
        'alert',
        'alert-dismissible',
        'alert-success'
    ));
  }
}
?>

Does anybody know about this error? This is first time, that this error occured. Google and "zend-mvc" - repo - issues says nothing about this.

Thanks for your attention!

  • 写回答

1条回答 默认 最新

  • duangan6133 2016-02-23 01:51
    关注

    See this in ZF2 (Best Pratices):

    The Plugin FlashMessenger , send your message to a waiting pool ( Through FlashMessenger Zend MVC Plugin ) which will be displayed on another page request ( Through ViewHelper FlashMessenger ) .

    There are 4 types of messages that you can integrate with the Bootstrap Notifications ( error, info , default , success ) .

    Now let's practice

    In Action within the Controller , you must enter your message and your brand :

    use Zend\Mvc\Controller\Plugin\FlashMessenger;
    
    public function registerAction(){
      if($formValid){
          $this->flashMessenger()->addSucessMessage('Saved!');
      } else{
          $this->flashMessenger()->addErrorMessage('Fail!');
      }
    
      //redirect to other route and show message
      return $this->redirect()->toRoute('app');
    }
    

    In View ( .phtml ) , you only need to use :

    #show messages of addErrorMessage();
    echo $flash->render('error',   array('alert', 'alert-dismissible', 'alert-danger'));
    #show messages of addInfoMessage();
    echo $flash->render('info',    array('alert', 'alert-dismissible', 'alert-info'));
    #show messages of addMessage();
    echo $flash->render('default', array('alert', 'alert-dismissible', 'alert-warning'));
    #show messages of addSucessMessage();
    echo $flash->render('success', array('alert', 'alert-dismissible', 'alert-success'));
    

    In View , if using Bootstrap :

     $flash = $this->flashMessenger();
     $flash->setMessageOpenFormat('<div>
         <button type="button" class="close" data-dismiss="alert" aria-hidden="true">
             &times;
         </button>
         <ul><li>')
         ->setMessageSeparatorString('</li><li>')
         ->setMessageCloseString('</li></ul></div>');
    
    
     echo $flash->render('error',   array('alert', 'alert-dismissible', 'alert-danger'));
     echo $flash->render('info',    array('alert', 'alert-dismissible', 'alert-info'));
     echo $flash->render('default', array('alert', 'alert-dismissible', 'alert-warning'));
     echo $flash->render('success', array('alert', 'alert-dismissible', 'alert-success'));
    

    Now 's a hack , if you want to view the FlashMessages on the screen without resquest ou redirect page ( Ideal for form errors , which you do not redirects or AJAX to another page ) , use this technique .

    public function registerAction(){
          if($formValid){
              $this->flashMessenger()->addSucessMessage('Saved!');
          } else{
              #add to pool
              $this->flashMessenger()->addErrorMessage('Fail');
              #merge message
              $feedback = array_merge(
                 $this->flashMessenger->getErrorMessages(),
                 $this->flashMessenger->getCurrentErrorMessages()
              );
              #clear pool
              $this->clearCurrentErrorMessages();
    
          }
    
          #message in variable to VIEW
          new ViewModel(array(
            'feedback' => $feedback,
          ));
        }
    
    }
    

    If you want to deepen better at it, follow the links Official Zend 2 documentation , gives a tried on available methods , will help a lot :

    VIEW -> http://framework.zend.com/manual/current/en/modules/zend.view.helpers.flash-messenger.html

    CONTROLLER -> http://framework.zend.com/manual/current/en/modules/zend.mvc.plugins.html#zend-mvc-controller-plugins-flashmessenger

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程