zend framework FlashMessenger problem

2019-05-23 16:12发布

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();
    }
}

5条回答
forever°为你锁心
2楼-- · 2019-05-23 16:24

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.

查看更多
在下西门庆
3楼-- · 2019-05-23 16:29

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.

查看更多
别忘想泡老子
4楼-- · 2019-05-23 16:31

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

查看更多
我只想做你的唯一
5楼-- · 2019-05-23 16:31

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;

查看更多
可以哭但决不认输i
6楼-- · 2019-05-23 16:36

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

查看更多
登录 后发表回答