4

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.

learnwhat
  • 177
  • 2
  • 15

2 Answers2

8

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:

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();
arogachev
  • 33,150
  • 7
  • 114
  • 117
2

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.

Paul LeJoy
  • 61
  • 1