glimr/response/redirect

Redirect Helpers

Builder pattern for creating HTTP redirects with support for flash messages and returning to previous pages. Use the builder to construct redirects before sending.

Types

Redirect builder for constructing HTTP redirect responses. Using this type constructor provides extra flexibility like optionally flashing messages or using a helper function to redirect back.

pub type Redirect {
  Redirect(path: String, flash_data: dict.Dict(String, String))
}

Constructors

  • Redirect(path: String, flash_data: dict.Dict(String, String))

Values

pub fn back(
  redirect: Redirect,
  req: request.Request(wisp.Connection),
) -> Redirect

Sets the redirect path to the previous page from the Referer header. Panics if no referer is found. Useful for cancel or back buttons that must have a referrer.

Example:

redirect.build()
|> redirect.back(req.request)
|> redirect.go()
pub fn build() -> Redirect

Creates a new redirect builder with empty path and flash data. Use this to start building a redirect response.

Example:

redirect.build()
|> redirect.to("/contact/success")
|> redirect.go()
pub fn flash(
  redirect: Redirect,
  flash_data: List(#(String, String)),
) -> Redirect

Adds a key-value pair to the flash data that will be stored in the session and available on the next request. Useful for success/error messages after redirects.

Example:

redirect.build()
|> redirect.to("/contact")
|> redirect.flash([#("success", "Message sent!")])
|> redirect.go()
pub fn go(redirect: Redirect) -> response.Response(wisp.Body)

Converts the redirect builder into an HTTP redirect response. This finalizes the redirect and sends it to the client. Flash data will be written to session when implemented.

Example:

redirect.build()
|> redirect.to("/contact/success")
|> redirect.go()
pub fn to(redirect: Redirect, path: String) -> Redirect

Sets the target path for the redirect. This is where the user will be sent when the redirect executes.

Example:

redirect.build()
|> redirect.to("/dashboard")
|> redirect.go()
Search Document