Phoenix.Controller.Connection

Handles Interacting with Plug.Conn and integration with the Controller layer

Used for sending responses and looking up private Conn assigns

Summary

action_name(conn)

Returns the Atom action name matched from Router

assign_error(conn, kind, error)

Assign error to phoenix private assigns

controller_module(conn)

Returns the Atom Controller Module matched from Router

error(conn)

Retrieve error from phoenix private assigns

html(conn, html)

Sends HTML response from provided html String

html(conn, status, html)
json(conn, json)

Sends JSON response from provided json String

json(conn, status, json)
layout(conn)

Retrieve layout from phoenix private assigns

put_layout(conn, layout)

Assign layout to phoenix private assigns

redirect(conn, url)

Sends redirect response to provided url String

redirect(conn, status, url)
response_content_type!(conn)

Returns the String Mime content-type of response

response_content_type(conn)

Returns the String Mime content-type of response

router_module(conn)

Returns the Actom Router Module that dispatched the Conn

send_response(conn, status, content_type, data)

Sends response to the client

text(conn, text)

Sends text response from provided String

text(conn, status, text)

Functions

action_name(conn)

Returns the Atom action name matched from Router

assign_error(conn, kind, error)

Assign error to phoenix private assigns

controller_module(conn)

Returns the Atom Controller Module matched from Router

error(conn)

Retrieve error from phoenix private assigns

html(conn, html)

Sends HTML response from provided html String

Examples

html conn, "<h1>Hello!</h1>"
html conn, 200, "<h1>Hello!</h1>"
html(conn, status, html)
json(conn, json)

Sends JSON response from provided json String

Examples

json conn, "{"id": 123}"
json conn, 200, "{"id": 123}"
json(conn, status, json)
layout(conn)

Retrieve layout from phoenix private assigns

put_layout(conn, layout)

Assign layout to phoenix private assigns

Possible values include any String, as well as the Atom :none to render without a layout.

Examples

iex> conn |> put_layout("print")
iex> conn |> put_layout(:none)
redirect(conn, url)

Sends redirect response to provided url String

Examples

redirect conn, "http://elixir-lang.org"
redirect conn, 404, "http://elixir-lang.org"
redirect(conn, status, url)
response_content_type(conn)

Returns the String Mime content-type of response

Examples

iex> response_content_type(conn)
{:ok, "text/html"}
iex> response_content_type(conn)
{:error, :unfetched}
response_content_type!(conn)

Returns the String Mime content-type of response

Raises Errors.UnfetchedContentType if content type is not yet fetched

router_module(conn)

Returns the Actom Router Module that dispatched the Conn

send_response(conn, status, content_type, data)

Sends response to the client

  • conn - the Plug Connection
  • status - The Integer or Atom http status, ie 200, 400, :ok, :bad_request
  • content_type - The String Mime content type of the response, ie, “text/html”
text(conn, text)

Sends text response from provided String

Examples

text conn, "hello"
text conn, 200, "hello"
text(conn, status, text)