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?...
Displaying flash messages. To check for flash messages we use the hasFlash() Method and to obtain the flash message we use the getFlash() Method. Since Yii v1. 1.3, there is also a method getFlashes() to fetch all the messages.
Session is a Web application component that can be accessed via Yii::$app->session . To start the session, call open(); To complete and send out session data, call close(); To destroy the session, call destroy(). Session can be used like an array to set and get session data.
In Yii, you can get access to sessions via the session application component. Step 1 − Add the actionOpenAndCloseSession method to the SiteController. In the above code, we get the session application component, open a session, check whether it is active, close the session, and finally destroy it.
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'); ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With