How do I use flash message in yii2?

how do I add a flash message to my site using the linked controller, please explain step by step, I'm new to php.

I use the yii2 structure to build the site, and I need to print the flash message on the index page using create controller.

+3
php
source share
2 answers

The question is currently too broad, but here is the main use:

1) You can install such a controller:

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

2) Then you can display it like this:

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

Optionally, you can check for availability with:

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

Official documents:

In fact, there are more ways to work with flashes, you can see it in official documents.

The extended template also provides a useful Alert widget that integrates with Boostrap 3:

 \Yii::$app->session->setFlash('error', 'This is the error message'); ... echo Alert::widget(); 
+5
source share

For a better understanding of working with flash messages, go to @ http://www.yiiframework.com . By following this method, you can print flash messages on your web page.

+2
source share

All Articles