samrose

samrose

Custom restructuring Phoenix Framework tradeoffs question

Looking for opinions.

I am in discussion with someone who would like to restructure phoenix project file structure so that model, view, controller, and tests will be all in one folder per named “feature” (let’s say “blog”)

I am looking for feedback on what the tradeoffs would be for doing this.

The argument for it is that “everything you need to find for the feature would be in one folder. This should be easier for everyone to find and use these files”. But are there fundamental reasons not to restructure a phoenix project in this way? I know it would lose compatibility with with code generators. However, would this also push people off track in their reasoning about he code? What are the impacts if any, for doing this?

Thanks!

Marked As Solved

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Controller / view code isn’t really this way though. I definitely agree with you that "model" code definitely doesn’t belong to a specific controller. However for controller / view stuff there’s a decent argument for favoring a “pod” structure.

While not exactly the same, LiveView definitely takes more of a “page” oriented structure since with a given live view + leex / heex template you generally have a bunch of functionality all right next to each other.

stefanchrobot

stefanchrobot

I use that approach in my app and I’m pretty happy with it! I don’t use generators though. I even have two separate “myapp_web.ex” files - one for the SSR app and one for the API:

- lib
  - foo
    - ...
  - foo_web
    - app
      - session
        - new.html.heex
        - session_controller.ex (use FooWeb.App, :controller)
        - session_view.ex
      - ...
    - mobile_api
      - message
        - message_controller.ex (use FooWeb.MobileApi, :controller)
        - message_view.ex
      - ...
    - app.ex
    - mobile_api.ex
BartOtten

BartOtten

In practice, it’s never about the extremes though. As there is a ‘data’ App and a separate ‘web’ AppWeb both styles can be used together.

If we look at how the phx generator creates the data app, we see vertical slicing as far as I can remember. Except for migrations which are all in one folder*. It’s the web app that (historically) uses horizontal slicing and although it can be beneficial, I rather use vertical slicing there too.

LiveView seems to move towards vertical slicing, with the template files next to the rest of the code for a page/view.

*Django puts migrations within the ‘feature’. Con: touch two features at once and you have two separated migration files.

Last Post!

stefanchrobot

stefanchrobot

I use that approach in my app and I’m pretty happy with it! I don’t use generators though. I even have two separate “myapp_web.ex” files - one for the SSR app and one for the API:

- lib
  - foo
    - ...
  - foo_web
    - app
      - session
        - new.html.heex
        - session_controller.ex (use FooWeb.App, :controller)
        - session_view.ex
      - ...
    - mobile_api
      - message
        - message_controller.ex (use FooWeb.MobileApi, :controller)
        - message_view.ex
      - ...
    - app.ex
    - mobile_api.ex

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
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement