How to work with flash messages

1,Setting flash messages(设置)

A flash message is used in order to keep a message in session through one or several requests of the same user.

Yii::app()->user->setFlash('success', "数据设置成功");

2,Displaying flash messages(显示)

To check for flash messages we use the hasFlash() Method and to obtain the flash message we use the getFlash() Method. Since Yii v1.1.3, there is also a method getFlashes() to fetch all the messages.


<?php if(Yii::app()->user->hasFlash('success')):?>
    <div class="info">
        <?php echo Yii::app()->user->getFlash('success'); ?>
<?php endif; ?>


$flashMessages = Yii::app()->user->getFlashes();
if ($flashMessages) {
    echo '<ul class="flashes">';
    foreach($flashMessages as $key => $message) {
        echo '<li><div class="flash-' . $key . '">' . $message . "</div></li>\n";
    echo '</ul>';


   '$(".info").animate({opacity: 1.0}, 3000).fadeOut("slow");',

With these lines of code we register a piece of jQuery (already included with YII) javascript code, using 'myHideEffect' as ID. It will be inserted in the jQuery's ready function (CClientScript::POS_READY).

comments powered by Disqus