Phoenix.Controller.Flash

Handles One-time messages, often referred to as “Flash” messages. Messages can be stored in the session and persisted across redirects for notices and alerts about request state.

Examples

def index(conn, _) do
  render conn, "index", notice: Flash.get(conn, :notice)
end

def create(conn, _) do
  conn
  |> Flash.put(:notice, "Created successfully")
  |> redirect("/")
end

Summary

call(conn, )

Clears the Message dict on new requests

clear(conn)

Clears all flash messages

get(conn)

Returns a message from the Phoenix.Flash

get(conn, key)

Returns a message from the Phoenix.Flash by key

get_all(conn, key)

Returns a list of messages by key from the Phoenix.Flash

init(opts)
pop_all(conn, key)

Removes all messages from the for given key, returning a {msgs, conn} pair

put(conn, key, message)

Persists a message in the Phoenix.Flash, within the current session

Functions

call(conn, )

Clears the Message dict on new requests

clear(conn)

Clears all flash messages

get(conn)

Returns a message from the Phoenix.Flash

Examples

iex> Flash.put(conn, :notice, "Hi!") |> Flash.get
%{notice: "Hi!"}
get(conn, key)

Returns a message from the Phoenix.Flash by key

Examples

iex> Flash.put(conn, :notice, "Hello!") |> Flash.get(:notice)
"Hello!"
get_all(conn, key)

Returns a list of messages by key from the Phoenix.Flash

Examples

iex> conn
|> Flash.put(:notice, "hello")
|> Flash.put(:notice, "world")
|> Flash.get_all(:notice)
["hello", "world"]
init(opts)
pop_all(conn, key)

Removes all messages from the for given key, returning a {msgs, conn} pair

Examples

iex> %Conn{}
|> Flash.put(:notice, "oh noes!")
|> Flash.put(:notice, "false alarm!")
|> Flash.pop_all(:notice)
{["oh noes!", "false alarm!"], %Conn{}}
put(conn, key, message)

Persists a message in the Phoenix.Flash, within the current session

Returns the updated %Conn{}

Examples

iex> conn = %Conn{private: %{plug_session: %{}}}
iex> match? %Conn{}, Flash.put(conn, :notice, "Welcome Back!")
true