zend framework FlashMessenger problem

2019-05-23 16:26发布

问题:

i am using FlashMessenger helper to set messages, but i am unable to retrieve messages using getMessages() method. it returns null. here is my sample code:

<?php
class Admin_TestController extends Zend_Controller_Action
{
    protected $_flashMessenger = null;

    public function init()
    {
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->initView();
    }

    public function myAction()
    {
        $this->_flashMessenger->addMessage('test message');

        $this->_helper->Redirector("mynextrequest");
    }

    public function mynextrequestAction()
    {
        zend_debug::dump($this->_flashMessenger->getMessages());
        $this->render();
    }
}

回答1:

To fix this problem you need to edit library/Zend/Controller/Action/Helper/FlashMessenger.php

find the line:
 self::$_session->{$this->_namespace}[] = $message;

and change it to
//self::$_session->{$this->_namespace}[] = $message;
$sessionMessages = self::$_session->{$this->_namespace};
$sessionMessages[] = $message;
self::$_session->{$this->_namespace} = $sessionMessages;

This issue only affects php 5.2 and so they have decided not to fix it and instead suggest upgrading the PHP version.



回答2:

i upgrade from php 5.2.0 to 5.2.9 and the problem solved.



回答3:

aside from the function/classname capitalization issues, make sure your Zend_Session is set up, started, and has a storage method that works. It will use your session storage method that comes from a new Zend_Session_Namespace('FlashMessenger')



回答4:

There are PHP 5.2.x Versions, that have a problem with

// Zend_Controller_Action_Helper_FlashMessenger::addMessage() (line 143)
self::$_session->{$this->_namespace}[] = $message;

Upgrading PHP would be a solution (as you did) or replacing the above line with the following code:

$messages = self::$_session->{$this->_namespace};

$messages[] = $message;

self::$_session->{$this->_namespace} = $messages;



回答5:

I had a problem that came from having two ajax calls that used the flashMessenger and were finishing loading in random order. So the first ajax call was sometimes loaded first and thus using the messages and left none for the second; and I was expecting the error messages in the second ajax call and wondered why do they show just approx 50% of the cases.