I want to write my own plug in my phoenix app. The plug is to check the cookies and render the error page if cookies don't exist. Similar to the 404 error. Based on the logic, those functions below may be called:
put_status
: set the status code of response, I set to 422.put_view
: My error page is located intemplates/error/422.html.eex
, so theErrorView
should be included.render
: Render the html page.halt
: halt the connection.
conn
|> put_status(422)
|> put_view(ErrorView)
|> render("422.html")
|> halt()
put_status
and halt
are in Plug.Conn
. put_view
and render
are in Phoenix.Controller
. I know that I can use the full namespace to call those functions, but it looks redundant. So I am considering import/use/alias
functions as well.
For Plug.Conn
, the Programming Phoenix 1.4 Book uses import​ Plug.Conn
, some official plug uses alias Plug.Conn
.
For Phoenix.Controller
, I haven't found any example, but import
could work. Is it not recommended to use Controller functions in Plug?
I am considering both the code simplify, readability and performance. Does anyone know what's the best practice for this?
Cheers