But when I do so I get an error telling me that the function ModWeb.MyView.render/2 is undefined : What I understand is that Phoenix tries to call render on some view associated with the controller, but I did not ask him to.
Should we always design things so that Controllers works with Views ?
If yes, what for instance if I want just to send some json in a request ? Also in this case, is there a natural place where to put the code which has neither to do with controllers, nor with schemas/database ?
But theyâre still proper plugs by themselves. Just because phoenix calls them at the end doesnât mean you cannot manually call them beforehand. If thatâs useful is a different question though
Controllers should work just fine without any Views.
What do you mean by âPhoenix calls them at the endâ ? Phoenix calls them when we ask Phoenix to call them, isnât that right ?
Otherwise, in my case, Controllers do not work fine without any views : Phoenix tries to call the render method of the view which is supposed to be attached to the controller, but which does not exists; and I donât know what causes that.
Iâm happy to only use Controller when I have views attached to them. With this question, I was hopping to understand a bit more how things work in Phoenix.
Uh, it shouldnât be doing that⊠What is a git url so we can try to git clone and test it out to see what is happening? Preferably with a test that directly demonstrates the issue?
Iâm not sure how to make it easy with a git url, especially dealing with the dependencies (which are quite many files by default). But the behavior I describe is quite easy to achieve by starting from a fresh project:
mix phx.new test --no-webpack
answer âYâ to installing dependencies
mix ecto.create
Then you can add this function into router.ex
defp authenticate_user(conn, _) do
case get_session(conn, :user_id) do
nil ->
conn
|> TestWeb.SessionController.call(:create_anonymous)
end
end
Oh if thatâs all you are doing then the SessionController will override your PageControllerâs settings as it injected first, you need to be sure to clear out the controller-specific settings (or just donât use use TestWeb, :controller or use Phoenixâs Controller at all). I bet itâs not the controller you are using as a plug that is failing but rather the render call or one of itâs kin at the controller you end up eventually accessing? What is your full stacktrace, that should always be shown.