See plug for more info since controllers are plugs
External data uses string keys, internal data uses atoms
Actions
All actions take two args:
conn: a struct which holds data about requests
params: request params(seems to everything like post body, query params, and more), Map with string keys
Return values with json/2, text/2, and html/2
To let Phoenix views build resp, use render/3
lib/hello_web/controllers/hello_controller.ex
defmoduleHelloWeb.HelloControllerdouseHelloWeb, :controllerimportHello.Auth, only: [logged_in_user: 2]#pass through plug with a guard(like middleware) plug :logged_in_user when action notin [:new, :create]#params are combo of /m/:messenger and query paramsdefshow(conn, %{"messenger"=> messenger}) do#gets :messenger from /hello/:messenger route#render(conn, "show.html", messenger: messenger) ORRRR conn|>assign(:messenger, messenger)|>render("show.html")#find and render show.html.eex in lib/hello_web/templates/hello (named after controller)enddefredirect(conn,_params) do conn |>put_flash(:info,"Redirecting")|>redirect(to: "/redirect_test")# |> redirect(external: "google.com")enddefget(conn,_params) do conn|>put_status(401)|>json(%{user: 1})# |> text("") #is empty 200 respendend
Flashes
Add server messages
conn|>put_flash(:info,"Welcome to Phoenix, from flash info!")|>put_flash(:error,"Let's pretend we have an error.")|>render("index.html")