acrolink
Should separate controllers be used to render HTML and JSON results for same entity types?
I have already created some models and controllers using mix phx.gen.html. I need to serve context as json. My question, should I create new controllers to serve json or is it better to modify current controllers to serve html/json depending on request type?
How to create new json controllers for existing models, what is the Pheonix / Mix way to do it?
And, inside say an index method, how to check request type and serve html/json accordingly? Thanks.
Marked As Solved
hubertlepicki
It’s one of these things that are nice in theory, but rarely work well in practice.
I would highly recommend separating your HTML-rendering controllers from API serving JSON. In my experience it usually becomes tiresome to maintain those two versions, as the HTML version will more than likely pull in more/different data than JSON version as time passes.
Also Liked
acrolink
mix phx.gen.json with --web option.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









