Yii 1.1: messagestack

A way to share messages between models, controllers and views
17 followers

This is a small, yet very useful component.

Many times, the models will handle all the processing logic and you will need a way to pass messages from the models back to controllers or even directly to your view.

This component does exactly that, once initialized, it will allow you to collect messages from everywhere and display them in any place you need to.

Because when adding a message, these are stored in key=>value pairs, this makes the component very useful when doing ajax validation and you want to return the response in a json format instead of calling CJSON::encode() followed by Yii::app()->end(); The advantage of using this component for this purpose is that, beside calling the above two methods, it will also append the correct headers for the json format.

Requirements

Tested on Yii 1.1.7

Usage

The component is very small, the following methods are available:

#set a message: 
Yii::app()->messageStack->set($messageKey, $messageValue);  
Yii::app()->messageStack->set(array($messageKey=>$messageValue));  
 
#get a message  
$msg=Yii::app()->messageStack->get($messageKey);  
 
#get all messages  
$messages=Yii::app()->messageStack->get();  
$messages=Yii::app()->messageStack->toArray();
 
#set a JSON response with full headers:  
Yii::app()->messageStack->set($arrayOfMessages)->toJson();  
#[this method will append the correct headers, will json_encode the message array and it will echo it, then it will call Yii::app()->end(); ]
 
#get the messages in a JSON format  
$messages=Yii::app()->messageStack->set($arrayOfMessages)->toJson(true);  
 
#remove all the stored messages  
Yii::app()->messageStack->reset();  
 
#check if we have some messages in the stack  
if(Yii::app()->messageStack->hasMessage())  
{  
//do something here 
}

Installation

In your main.php config file in the components area, add :

'messageStack' => array(  
            'class' =>  'CmsMessageStack',  
        ),

Total 3 comments

#4078 report it
twisted1919 at 2011/06/04 04:59am
To answer your question.

This component is used for the current request, doing a redirect will end up with nothing in the message stack, the purpose of this component is to collect messages from your application from distinct places during a request and in the end, to use those messages as you want .
If you want your messages to persist during page reload, Yii::app()->user->setFlash() is the way to do it. There is no use to make this component doing same job that Yii is already doing .

#4057 report it
ryurhrt at 2011/06/01 09:54pm
Some Question

Hi,

I would like to have a place to push my error/success/warning/info messages, so then I could call it for notification to users using jNotify.

I found the yii framework's Flash message doesn't really work out for me, as i need a place to push the messages in, and the users will then received the notification at any place and any page. (i will put the message pop out code in layout)

so i am not sure what is the different between this messagestack and the yii user flash?

and can this modules doing something like below:

i can push a object look like object('type'=>'error','message'=>'the error msg'), then i could push another object at another function let say like object('type'=>'warning','message'=>'the warning msg')

during reload/redirect ,i can read these object of messages, and pop up to the users, you get what i means?

#3813 report it
waterloomatt at 2011/05/10 03:40am
Very nice and clean

Thanks you!

Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: Other Open Source License
  • Developed by: twisted1919
  • Category: Others
  • Votes: +10
  • Downloaded: 455 times
  • Created on: May 3, 2011
  • Last updated: May 3, 2011
  • Tags: messages, message stack