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',
),
Very nice and clean
Thanks you!
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?
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 .
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.