Search code examples
phpzend-framework

Zend Framework: Request seems to be outdated by 1 request


I seem to experiencing a weird error where my request params seem to be outdated by 1 request

my login code @ pastebin

I have tried to illustrate my point in a video @ screenr


Solution

  • It works exactly like it should, this is the intended behavior of FlashMessenger helper.

    What FlashMessenger does is store the messages you add in session. It only loads them from the session upon constructing a new FlashMessenger object; that is when the next request occurs. This is the whole point of FlashMessenger; to pass messages from one request to the another; not to pass messages within the same request (for that, you just use regular variables, or storage such as Zend_Registry).

    What you usually do with FlashMessenger is that you first add a message, and you then redirect to the same (or another) page, using eg. the _redirect method, or the Redirector action helper. It's convenient to subclass the Zend_Controller class, and add your own helper method that lets you do both of these in single method, such as a method called _flash. For instance I usually do something like this:

    <?php
    
    class App_Controller extends Zend_Controller_Action
    {
        protected $_messages = array();
    
        protected function _flash($message, $to = false)
        {
            $flashMessenger = $this->_helper->getHelper('FlashMessenger');
            if ($message)
                $flashMessenger->addMessage($message);
            if (!$to)
                $to = $this->_request->getRequestUri();
            $this->_redirect($to);
        }
    
        public function postDispatch()
        {
            // Set flash messages
            $flashMessenger = $this->_helper->getHelper('FlashMessenger');
            $this->view->messages = array_merge($this->_messages, $flashMessenger->getMessages());
        }
    }
    

    I then simply do $this->_flash("My message"); which will do two things: 1) Add the message to the flash message stack and 2) Refresh the page.

    You can also see that I have a protected $_messages, the reason for this is that if I want to display a message without having to refresh the page, I can simply append a message to that array.