How to use Yii2 Flash Message?

yii2 flash message
Yii2 Flash Message

Yii2

How to use Yii2 Flash Message?

Learn how to use Yii2 Flash Message in basic and advanced template

Basically Flash message is a message that shows on the top of any page indicating any error, warning or success notification.

In Yii2 you can send Flash Message from controller to views along with custom message after setting it using session.

Set your Flash message before redirecting or rendering to any view with two parameters, 1st one will indicate type of message  success or  error and other parameter holds your message.

Here is how to achieve this.

If you are using Yii2 advanced template you will find a widget in your layout/main.php

?= Alert::widget() ?>

This Alert Widget will receive Flash Message and show according to parameters. If you want to show your flash message somewhere else just remove from your layout and call it in any other view.

If you are using Yii2 basic template you can use like this.

SetFlash() in Controller

Check if session hasFlash() Message

Then getFlash() and show in View

 

I am a Sr. Android App & Web Developer having 5 plus years of extensive hands-on experience in Object Oriented Programming, Database Design and Implementation using Agile & UML. Pro in Yii2 PHP Framework, Android App Development, MYSQL & a Blockchain enthusiast.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

More in Yii2

Recent Posts

Categories

Tags

To Top