35

I have a problem with setting flash messages. So, i have an action which in some cases should redirect with flash. It looks like this:

if(!$this->_isSameOrg($reports)){
    \Yii::$app->session->setFlash('consol_v_error',\Yii::t('app/consol', 'some_text'));
    $this->redirect(\Yii::$app->request->getReferrer());
    return;
}

After redirect in view i have this

<div class="col-lg-12">
    <?php if(Yii::$app->session->hasFlash('consol_v_error')): ?>
        <div class="alert alert-danger" role="alert">
            <?= Yii::$app->session->getFlash('consol_v_error') ?>
        </div>
    <?php endif; ?>
</div>

The problem is i don't see any message here. In Debug panel i see SESSION var populated with good flash, but it doesn't display with this if-statement. Maybe i need to configure session component or something?...

lin
  • 17,956
  • 4
  • 59
  • 83
Anton Abramov
  • 2,251
  • 6
  • 24
  • 27

8 Answers8

45

To set flash,try like

  \Yii::$app->getSession()->setFlash('error', 'Your Text Here..');
   return $this->redirect('Your Action');

And to display it..

   <?= Yii::$app->session->getFlash('error'); ?>
Dency G B
  • 8,096
  • 9
  • 47
  • 78
  • That's because the flash returned is in an array. You may have got the following error: PHP Notice – yii\base\ErrorException Array to string conversion – pkanane Jan 14 '16 at 18:23
30

you can try like this

<?php
foreach (Yii::$app->session->getAllFlashes() as $key => $message) {
echo '<div class="alert alert-' . $key . '">' . $message . '</div>';
}
?>
Andi Fitria
  • 439
  • 4
  • 4
  • 2
    Very good solution to use a global flash section - in the layout for example- to render any flash message. +1 – SaidbakR May 26 '15 at 03:53
  • It's not a good solution, because it retrives ALL the session messages of the application. If you want to implement in a single part of the site the good way to do it is with this = Yii::$app->session->getFlash('error'); ?> – giovaZ Dec 15 '15 at 14:01
14

Simply do:

  1. Add two strings to: /views/layout/main.php

    • in block use:

    use frontend\widgets\Alert;
    
    • before <?= $content ?>:

    <?= Alert::widget() ?>
    
  2. Now all messages automatically will be on screen. Lets try it! Add in any controller's method:
Yii::$app->session->setFlash('warning', 'bla bla bla bla 1');

Yii::$app->session->setFlash('success', 'bla bla 2');

Yii::$app->session->setFlash('error', 'bla bla 3');
Teivaz
  • 5,462
  • 4
  • 37
  • 75
  • 4
    I prefer this over the other suggestions. In my case I needed to add this widget to my project since I based my install off of the Basic template. https://github.com/yiisoft/yii2-app-advanced/blob/master/common/widgets/Alert.php – CGSmith105 Mar 19 '17 at 17:16
9

Instead of this:

$this->redirect(\Yii::$app->request->getReferrer());

return;

try this:

return $this->redirect(\Yii::$app->request->getReferrer());

It's working fine for me.

arogachev
  • 33,150
  • 7
  • 114
  • 117
3

in yii2 flash can be set like this

Yii::$app->session->setFlash('success', 'Thank you ');
sprytechies
  • 455
  • 2
  • 8
1

Here is my solution: overwrite standart Session class:

namespace app\components;

use Yii;

class Session extends \yii\web\Session {

    public function getAllFlashesNormalized() {
        $flashes = [];
        foreach (Yii::$app->session->getAllFlashes() as $key => $flash) {
            if (is_array($flash))
                foreach ($flash AS $message)
                    $flashes[] = ['key' => $key, 'message' => $message];
            else
                $flashes[] = ['key' => $key, 'message' => $flash];
        }

        return $flashes;
    }
}

So you can:

Yii::$app->session->addFlash('success', 'Text.');
Yii::$app->session->addFlash('success', 'Another text.');

And output this messages:

<?php foreach (Yii::$app->session->getAllFlashesNormalized() as $flash) { ?>
    <div class="alert alert-<?=$flash['key']?>" role="alert"><?=$flash['message']?></div>
<?php } ?>
Turako
  • 614
  • 7
  • 11
0

in my case flash message deleted after redirect, when i use hasFlash before redirect.

if (!Yii::$app->getSession()->hasFlash('success')) {
    Yii::$app->getSession()->setFlash('success', Yii::t('app', 'your text'));
}  

So i added this and it helped

if (!Yii::$app->getSession()->hasFlash('success')) {
    Yii::$app->getSession()->setFlash('success', Yii::t('app', 'your text'));
} else {
    Yii::$app->getSession()->set('__flash', array('success' => -1));
} 
Azamat
  • 1
  • 1
-1

Did not work for me. I'd rather use:

In the controler:

$session = new Session;
$session->addFlash("warning","Your text here");

In the view :

<?php 
$session = new Session;
foreach ($session->getAllFlashesNormalized() as $flash) { 
?>
<div class="alert alert-<?=$flash['key']?>" role="alert">
    <?=$flash['message']?>
</div>
<?php 
} 
?>
dom
  • 87
  • 3