garj

garj

Phoenix app with both html and json end point

Hi,

My phoenix app currently supports server side rendering. I would like it to support json end point as well. I know I can easily do this in router.ex by adding a scope: “/api/” and pipe_through api plug. However I do not know if I should create seperate controllers for the json app or I should modify existing controllers and render different view depending on the request. Any convention or thoughts related to this? Thanks!

Most Liked

OvermindDL1

OvermindDL1

You don’t need to pattern match in the controller. Phoenix already handles different views based on what the browser requests. For example when I do render(conn, :request, blah_args: blah_args) then it will render my "index.html" template called via render if html is requested, or it will call my render function that is matched on "index.json" for the JSON conversion (I also have others too). :slight_smile:

hubertlepicki

hubertlepicki

From my experience, the same controllers returning JSON view only works when the app is really simple CRUD-type of thing.

The needs of API client (either JS or mobile) are usually more specific, and you may want to optimize how and what data is being sent to those clients. This could result in messy controller code.

Go with the same controllers only when the API and pages you show match really well, and the thing is really simple.

If in doubt - build separate controllers. This does not hurt that much, sometimes more code is better than less - but convoluted code.

lukerollans

lukerollans

Hey there!

In my opinion, you should definitely create separate controllers for your API pipeline. As you’ve already said, setup your router to scope on /api or similar and pipe_through :api, where your api pipeline consists of at least plug :accepts, ["json"]

The alternative would be to pattern match on conn in a single controller and either render HTML or JSON views depending on the format request header or something. To me, this mixes concerns a bit too much and makes your pipelines much less flexible.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement