magosi

magosi

Writing modular / composable phoenix code

Hi, my current Phoenix project involves a website with a lot of different functionality, like a digital store, chat system, file uploads, blog posts, comments, etc. How can I go about developing this project so that each feature is a self-contained unit that I can reuse in future projects? For example, how can I make the digital store (with its contexts, views, templates, routes, etc) a unit that can plug into the project and plug into future projects? I know Django has a concept called apps that serves this purpose. Does Phoenix have a similar feature? I have seen people mention umbrella projects and I have made a small umbrella project before when I read the book “Programming Phoenix”, but I am not sure how to go about splitting these features so that they are almost pluggable. Any advice is appreciated and I would like to know how some of the more experienced Phoenix developers reuse code between projects to avoid reimplementing functionality.

Most Liked

wolf4earth

wolf4earth

AFAIK there is no support for this out of the box but it should be doable.


Assuming you really want to have multiple Phoenix apps, then going down the umbrella app route is most likely the best approach.

For that you would have one “main” app, and a number of “sub” apps (just by name though, from the umbrella perspective they are all equal).

Your “main” app would then depend on all these “sub” apps and actually serve requests. In the main apps router you could then use forward to forward the request to the router of the correct “sub” app. From here on it’s business as usual.

In the “sub” apps you would merely disable the server option of the Phoenix endpoint (in the config, the default is false), so that they do not bind to a port.

If you then at any point in the future want to extract a “sub” app into a standalone app you would have to set the server option back to true (and of course clean up the umbrella stuff, there are guides for this).

Does this help you?

fireproofsocks

fireproofsocks

I have re-used modules many times from different Phoenix projects: they are very portable. The only wiring that was needed to install them was to reference them somewhere (e.g. in a plug or a controller). It’s not a plug-and-play as, say, a WordPress plugin, but 1 size doesn’t always fit all. Sometimes the “simplicity” in having things as easy to install as a WordPress plugin backfires when you need to customize it, and jail-breaking the functionality out of its carefully defined environment is harder than just wiring in more generic modules.

Phoenix Plugs might be the middle ground there: they revolve around a well-documented struct and are easy enough to reference in various projects. They can be used to handle an entire request, from soup to nuts, so I would recommend giving them a look before trying to balance multiple Phoenix apps under an umbrella or some other more sophisticated solution.

egze

egze

Yeah, I would not bother with such strict boundaries for a simple application.

Btw, have a look at this video: https://www.youtube.com/watch?v=hgDCg3QdZgU

Last Post!

magosi

magosi

Thank you very much. I will certainly check it out.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement