Is it good to use Phoenix.Controller in Plug?

Hi all, I am writing the custom module plug in my Phoenix app. The plug includes functions in Phoenix.Controller, i.e put_view, `render, etc.

I can import Phoenix.Controller to the plug, but I haven’t saw any courses or tutorial to do it, is it a good practice?

|> put_status(422)
|> put_view(ErrorView)
|> render("422.html")
|> halt()

I also meet the issue to test the plug. It shows me the error:

** (KeyError) key :phoenix_endpoint not found in: %{phoenix_recycled: true, phoenix_template: "422.html", ..,}

Does anyone have experience on it?

1 Like


Regarding the error, try adding @endpoint DubberConnectWeb.Endpoint to the test module.

1 Like

Thanks @idi527, but it doesn’t work. Actually the @endpoint will be automatically load by use MyAppWeb.ConnCase.

Instead, I use the bypass_through and make the test pass. But I don’t know why.