Yii 1.1: simple-flash-message

Simple Flash Message is a widget for render web user flash messages with auto hide effect using JQuery.
8 followers

Requirements

Yii 1.1 or above

Usage

To set up the widget, download and extract under protected/components folder.

To render the widget for all view, you need to add the following code snippet to your main layout, place it between breadcrumbs and content:

............
        <?php if(isset($this->breadcrumbs)):?>
        <?php $this->widget('zii.widgets.CBreadcrumbs', array(
            'links'=>$this->breadcrumbs,
        )); ?><!-- breadcrumbs -->
    <?php endif?>
 
    // this is the widget
    <?php $this->widget('Flashes'); ?>
 
    <?php echo $content; ?>
        ............

Example usage in controller: Just like using original setFlash Yii function. Types of messages that can be used is 'success', 'error', and 'notice'.

........
        public function actionUpdate($id)
    {
    $model=$this->loadModel($id);   
        if(isset($_POST['Post']))
        {
        $model->attributes=$_POST['Post'];
        if($model->save()){
                // here is the code to set flash message
                    Yii::app()->user->setFlash('success','Save data successfully');
                    $this->redirect(array('view', 'id'=>$model->id));
            }       
        }
        $this->render('update',array(
            'model'=>$model,
        ));
    }
        ...........

Total 1 comment

#6715 report it
Jgsaw at 2012/01/31 10:03am
Basic but useful

Not a huge gain but as a page load performance gain I would move the if ($flashMessages) check up above the register JS part as theres no point registering the JS if there are no flashes. I aslo made style configurable if desired (although possibly overkill) i.e.

$flashMessages = Yii::app()->user->getFlashes();
    if ($flashMessages) {
    Yii::app()->clientScript->registerScript(
       'myHideEffect',
       '$(".flashes").animate({opacity: 0.9}, 3500).fadeOut("slow");',
       CClientScript::POS_READY
    );
 
    echo '<div class="flashes" style="'.$this->style.'">';
    foreach($flashMessages as $key => $message) {
        echo '<div class="flash-' . $key . ' shadow">' . $message . "</div>\n";
    }
    echo '</div>';
}
public $style = 'text-align:center; padding:5px 20px 0px 20px';

Leave a comment

Please to leave your comment.

Create extension
Downloads