Revel provides two cookie-based storage mechanisms for convenience, Session and Flash.
// A signed cookie, and thus limited to 4kb in size.
// Restriction: Keys may not have a colon in them.
type Session map[string]string
// Flash represents a cookie that gets overwritten on each request.
// It allows data to be stored across one page at a time.
// This is commonly used to implement success or error messages.
// e.g. the Post/Redirect/Get pattern: http://en.wikipedia.org/wiki/Post/Redirect/Get
type Flash struct {
Data, Out map[string]string
}
NOTE: To set your own cookie, use Controller.SetCookie()
func (c MyController) MyMethod() revel.Result {
new_cookie := &http.Cookie{Name: "foo", Value: "Bar"}
c.SetCookie(new_cookie)
return c.Render()
}
Session
Revel’s concept of session is a string map, stored as a cryptographically signed cookie.
This has some implications:
- The size limit is 4kb.
- All data must be serialized to a
string
for storage. - All data may be viewed by the user as it is not encrypted, but it is safe from modification.
The default lifetime of the session cookie is the browser lifetime. This can be overriden to a specific amount of time by setting the session.expires option in conf/app.conf. The format is that of time.ParseDuration.
func (c MyController) MyMethod() revel.Result {
c.Session["foo"] = "bar"
c.Session["bar"] = 1 // Error - value needs to be a string
delete(c.Session, "abc") // Removed item from session
return c.Render()
}
Flash
The Flash provides single-use string storage. It is useful for implementing the Post/Redirect/Get pattern, or for transient “Operation Successful!” or “Operation Failed!” messages.
Here’s an example of that pattern:
// Show the Settings form
func (c App) ShowSettings() revel.Result {
return c.Render()
}
// Process a post
func (c App) SaveSettings(setting string) revel.Result {
// Make sure `setting` is provided and not empty
c.Validation.Required(setting)
if c.Validation.HasErrors() {
// Sets the flash parameter `error` which will be sent by a flash cookie
c.Flash.Error("Settings invalid!")
// Keep the validation error from above by setting a flash cookie
c.Validation.Keep()
// Copies all given parameters (URL, Form, Multipart) to the flash cookie
c.FlashParams()
return c.Redirect(App.ShowSettings)
}
saveSetting(setting)
// Sets the flash cookie to contain a success string
c.Flash.Success("Settings saved!")
return c.Redirect(App.ShowSettings)
}
Walking through this example:
- User fetches the settings page.
- User posts a setting (POST)
- Application processes the request, saves an error or success message to the flash, and redirects the user to the settings page (REDIRECT)
- User fetches the settings page, whose template shows the flashed message. (GET)
It uses two convenience functions:
Flash.Success(message string)
is an abbreviation ofFlash.Out["success"] = message
Flash.Error(message string)
is an abbreviation ofFlash.Out["error"] = message
Flash messages may be referenced by key in templates. For example, to access the success and error messages set by the convenience functions, use these expressions:
{{.flash.success}}
{{.flash.error}}
Here is a second scenario where you want the flash variables returned without using a redirect
func (c Controller) Submit(Input UserName) {
Input.Validate(c.Validation);
if c.Validation.HasErrors() {
data := map[string]string{}
for key, vals := range c.Params.Values {
data[key] = strings.Join(vals, ",")
}
c.RenderArgs["flash"] = data
// Display input page
return c.RenderTemplate("test.html")
}
...
}