Issues

ZF-98: Messages beetwen actions

Description

I was wondering about the Controller/Actions loop in Front Controller, and I think that can be useful an embedded messenger subsystem, toward to improve the communication beetwen actions - for example (in the real world) some use case actions.

Actually this can be done by singleton register method (Zend::register and Zend::registry), registering a class to restore previous messages (errors, informations, confirmations, ...).

By doing this, the message can be raised anywhere during the action:


$this->raiseMessage(' The message itself ', ActionMessage::ERROR_MESSAGE);

I've coded a simple class to get the idea (and maybe for snippet code):


<?php
/**
 * Class for Handling Messages beetwen actions
 *
 * @copyright  Copyright (c) 2006
 * @license    GPL
 */
class ActionMessage {
    private $messages = array();
    private $errors = array();
    private $confirmations = array();
    
    const ERROR = "error"; 
    const CONFIRMATION = "confirmation"; 
    const MESSAGE = "message"; 
    
    public function appendTo($msg, $type = self::MESSAGE) {
        if ($type == self::ERROR) {
            $this->errors[]['msg'] = $msg ;
        }
        else if ($type == self::CONFIRMATION) {
            $this->confirmations[]['msg'] = $msg ;
        }
        else if ($type == self::MESSAGE) {
            $this->messages[]['msg'] = $msg ;
        }
    }
    
    public function haveMessagesAt($type = self::MESSAGE) {
        if ($type == self::ERROR) {
            return (count($this->errors) > 0) ? true : false ;
        }
        else if ($type == self::CONFIRMATION) {
            return (count($this->confirmations) > 0) ? true : false ;
        }
        else if ($type == self::MESSAGE) {
            return (count($this->messages) > 0) ? true : false ;
        }
    }
    
    public function getAllErrors() {
        return $this->errors ;
    }
    
    public function getAllMessages() {
        return $this->messages ;
    }
    
    public function getAllConfirmations() {
        return $this->confirmations ;
    }
    
    public function getLastError() {
        return $this->errors[count($this->errors)-1] ;
    }
    
    public function getLastMessage() {
        return $this->messages[count($this->messages)-1] ;
    }

    public function getLastConfirmation() {
        return $this->confirmations[count($this->confirmations)-1] ;
    }   
    
    
}

?>

My best regards...

Daniel S. W.

Comments

Messages can be passed by using the $params argument of Zend_Controller_Action::_forward().

Doing bookkeeping. Not sure why this was reopened. Might have been a mistake I made. Please reopen with a comment if it really isn't resolved.

As Mike noted, messages can be passed using the request object parameters during _forward(). Additionally, if you need "flash" messages between requests, we now have the FlashMessenger action helper. Anything beyond this would require a formal proposal at this point.

Greetings All,

FYI, I've created a PriorityMessenger which I believe fulfills the need being addressed here - please check it out and tell me what you think? http://emanaton.com/code/php/zendprioritymessenger

Regards,

Sean P. O. MacCath-Moran www.emanaton.com