Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how I use flash message in yii2?

Tags:

php

how I add flash message to my site using related controller please explain step by step i'm new to the php.

I use yii2 framework to build the site and i need to print flash message in the index page using create controller.

like image 336
learnwhat Avatar asked Oct 27 '25 02:10

learnwhat


2 Answers

Currently the question is too broad, but here is basic usage:

1) You can set in controller like that:

\Yii::$app->session->setFlash('flashMessage', 'Hello world!');

2) Then you can display it in view like so:

echo \Yii::$app->session->getFlash('flashMessage');

Optionally you can check the existence with:

\Yii::$app->session->hasFlash('flashMessage');

Official docs:

  • Session
  • setFlash()
  • hasFlash()
  • getFlash()

There are actually more methods for working with flashes, you can see it in official docs.

Also advanced template provides useful widget Alert that integrated with Boostrap 3:

\Yii::$app->session->setFlash('error', 'This is the error message');

...

echo Alert::widget();
like image 86
arogachev Avatar answered Oct 28 '25 17:10

arogachev


For better understanding of working with flash messages, go through @ http://www.yiiframework.com. By following this method, you can done printing flash messages in your web page.

like image 43
Paul LeJoy Avatar answered Oct 28 '25 16:10

Paul LeJoy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!