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,
));
}
...........
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';
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.