messagestack A way to share messages between models, controllers and views

  1. Requirements
  2. Usage
  3. Installation

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',  
        ),  
10 0
15 followers
554 downloads
Yii Version: 1.1
License: (not set)
Category: Others
Developed by: twisted1919
Created on: May 3, 2011
Last updated: 7 years ago

Downloads

show all

Related Extensions