Controllers in Phoenix
In Phoenix, controllers are plugs. This is an extension of the plug philosophy that we covered in Chapter 3, where a web request goes through a pipeline of plugs that transform the connection, the %Plug.Conn{}
struct, to indicate that a plug was called. Therefore, a router and a controller are both plugs responsible for transforming the connection and responding to the end user.
A controller is a module that contains several request handlers as functions. A Phoenix controller is called by a router based on the route of the incoming request. The request is also properly delegated to a particular request handler function in the controller depending on the route defined in the router. The controller functions that are responsible for handling a request are called actions
. By default, a Phoenix controller’s action has an arity of 2. It takes a Plug.Conn
struct as the first argument and request parameters as a Map
as the second argument.
Here’s...