Learn how to create and use function plugs in Phoenix for middleware functionality
Learn how to create and use function plugs in Phoenix for middleware functionality.
In Phoenix, a plug is like middleware: it receives a request connection (conn
), does something with it, and passes it along.
conn
(connection) and opts
(optional params)check_promo_code
plug that checks for a promo
code in the URL querypromo: true
) to the conn
, making it accessible in your viewsassign(conn, :key, value)
to attach data; this can be rendered conditionally on the frontend using HEEx templatesendpoint
or the router
and run at each request stepconn.params
for cleaner, more idiomatic codeopts
argument@promo
in HEEx to display a message if a valid promo code exists, and render conditionally with :if
This lesson covers building a custom function plug in Phoenix, assigning values to requests, conditionally rendering frontend content with HEEx, and using Elixir pattern matching for clean plug code.