Search code examples
phpyii2yii-widgets

Yii alert widget


In view I have widget:

if ($content):
        echo Alert::widget([
            'options' => [
                'class' => 'alert-info',
            ],
            'body' => $content,
        ]);
endif;

That widget I want render not always, for example after save and atc. Now Now I have placed that widget between if condition, maybe exists some more clear way to render widget only in some cases.


Solution

  • I think Flash-Messages is what you want:

    For Example:
    In the controller you can do something like that:

    <?php
    Yii::app()->user->setFlash('success', "Data saved!");
    $this->redirect(array('thing/view', 'id' => 1));
    

    And in the view:

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

    And of course you can combine it with the alert-widget or with a custom-widget.

    See full documentation: http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/