Building a controller interface
In this section, we will learn what it takes to build a controller by leveraging the Plug package and everything we’ve learned about it.
To build our own controller, let’s start by creating a new Mix project by running the following command:
$ mix new goldcrest
Now, our goal is to be able to write a controller that looks something like a Phoenix controller:
defmodule ExampleController do # .. TODO: use/import modules def greet(conn, _params) do conn |> put_status(200) |> render(:json, %{data: "hello world"}) end end
The previous controller defines one action, greet/2
, which responds with a status of 200
along with the {"data": "hello world"}
JSON body.
We have hidden the complexity of the use
or import
modules to introduce controller-like behavior to our module, but we will figure...