Yii2 Session, Flash messages
Asked Answered
N

8

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?...

Nursemaid answered 18/8, 2014 at 15:49 Comment(2)
This might have to do with the redirect, try rendering a test view instead of $this->redirect(\Yii::$app->request->getReferrer()); and displaying the flash message there.Door
do you use Advance Template ?Schinica
Y
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'); ?>
Yoshida answered 19/8, 2014 at 11:14 Comment(1)
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 conversionLux
S
30

you can try like this

<?php
foreach (Yii::$app->session->getAllFlashes() as $key => $message) {
echo '<div class="alert alert-' . $key . '">' . $message . '</div>';
}
?>
Swirl answered 27/10, 2014 at 15:18 Comment(2)
Very good solution to use a global flash section - in the layout for example- to render any flash message. +1Retharethink
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'); ?>Charlatanry
C
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');
Carbamate answered 10/5, 2016 at 13:49 Comment(1)
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. github.com/yiisoft/yii2-app-advanced/blob/master/common/widgets/…Compatriot
S
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.

Salenasalene answered 9/5, 2015 at 22:0 Comment(1)
Could you elaborate on why this is working? (Why should we return the value?). Thanks!Cornall
W
3

in yii2 flash can be set like this

Yii::$app->session->setFlash('success', 'Thank you ');
Wini answered 20/8, 2014 at 5:54 Comment(0)
S
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 } ?>
Serotherapy answered 19/1, 2016 at 14:36 Comment(0)
A
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));
} 
Astyanax answered 12/2, 2019 at 7:48 Comment(0)
M
-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 
} 
?>
Mccusker answered 4/2, 2016 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.