Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flash Message in YII Framework

Tags:

yii

After I send the request, I will announce the result to the user via SET FLASH. What is the way to show a message when the user sends a request?

For example when sending a message form : Display -> form is being send and then a flash message is displayed

like image 792
user2442901 Avatar asked Dec 25 '22 22:12

user2442901


2 Answers

Check the wiki on the Yii framework website: http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/

In your controller you can put:

Yii::app()->user->setFlash('success', "Form posted!");

In your view you can echo the flash message by:

<?php echo Yii::app()->user->getFlash('success'); ?>

Optionally you can check if a flash message exists by using the hasFlash method, so the code in your view would look like this:

<?php if(Yii::app()->user->hasFlash('success')):?>

      <?php echo Yii::app()->user->getFlash('success'); ?>

<?php endif; ?>
like image 96
davey Avatar answered Feb 01 '23 17:02

davey


Add setFlash in your controller. Something like this:

if($comment->save())
{
    Yii::app()->user->setFlash('commentSubmitted','Thank you for your comment.');
    $this->refresh();
}

And in your views, display flash message something like this:

<?php if(Yii::app()->user->hasFlash('commentSubmitted')): ?>
    <div class="flash-success">
        <?php echo Yii::app()->user->getFlash('commentSubmitted'); ?>
    </div>
<?php endif; ?>
like image 31
Mohit Bhansali Avatar answered Feb 01 '23 19:02

Mohit Bhansali