Phoenix.Controller
Phoenix.Controller
Controllers are used to group common functionality in the same (pluggable) module.
For example, the route:
get "/users/:id", MyApp.UserController, :show
will invoke the show/2
action in the MyApp.UserController
:
defmodule MyApp.UserController do
use MyApp.Web, :controller
def show(conn, %{"id" => id}) do
user = Repo.get(User, id)
render conn, "show.html", user: user
end
end
An action is just a regular function that receives the connection and the request parameters as arguments. The connection is a Plug.Conn
struct, as specified by the Plug library.
Connection
A controller by default provides many convenience functions for manipulating the connection, rendering templates, and m