Flash Messages
Sometimes you need to temporarily store data between requests of the same user, such as error or success messages after a form submission. The Iris sessions package supports flash messages.
As we've seen the Sessions chapter, you initialize a session like this:
1
import "github.com/kataras/iris/v12/sessions"
2
3
sess := sessions.New(sessions.Config{Cookie: "cookieName", ...})
Copied!
1
// [app := iris.New...]
2
3
app.Get("/path", func(ctx iris.Context) {
4
session := sess.Start(ctx)
5
// [...]
6
})
Copied!
The Session contains the following methods to store, retrieve and remove flash messages.
1
SetFlash(key string, value interface{})
2
HasFlash() bool
3
GetFlashes() map[string]interface{}
4
5
PeekFlash(key string) interface{}
6
GetFlash(key string) interface{}
7
GetFlashString(key string) string
8
GetFlashStringDefault(key string, defaultValue string) string
9
10
DeleteFlash(key string)
11
ClearFlashes()
Copied!
The method names are self-explained. For example, if you need to get a message and remove it at the next request use the GetFlash. When you only need to retrieve but don't remove then use the PeekFlash.
Flash messages are not stored in a database.
Last modified 1yr ago
Export as PDF
Copy link