cevado

cevado

Should Phoenix follow Elixir naming standards by default?

when using phoenix generators it creates MyAppWeb.ResourceController and put it inside /lib/my_app_web/controllers/resource_controller.ex file. I was thinking if it wouldn’t be better if it followed the elixir standards and have a MyAppWeb.Controllers.Resource in a /lib/my_app_web/controllers/resource.ex file.

I understand that this is done that way so phoenix can abstract the default view from the controller module name, but it could be an option passed to MyAppWeb when using it. Something like use MyAppWeb, controller: [default_view: ViewModule].

Most Liked

chrismccord

chrismccord

Creator of Phoenix

@dimitarvp and @al2o3cr explained it beautifully. The alias/tab problem is untenable when you’d have a user.ex and User for the controller, the view, the struct, etc, and all the tests and what not. As a bit of history, I took the Controllers.User approach way back when I first started Phoenix and José is the one that recommended this change. So “standards” wise, this isn’t even a concern :slight_smile:

hauleth

hauleth

This is exactly the approach I am using in my projects. Example controller. I really do not like the default approach where the “regular” naming scheme is broken. Thanks to that approach it router is IMHO much cleaner:

scope "/", LangustaWeb.Controllers do
  pipe_through :browser

  get "/", Stories, :index
  get "/newest", Stories, :newest
  get "/newest/:name", Stories, :newest

  get "/s/:id/*rest", Stories, :show
  get "/t/:tags", Stories, :for_tags

  get "/u/:name", Account, :profile
  get "/login", Account, :login_form
  post "/login", Account, :login

  scope "/" do
    pipe_through :logged_in

    post "/s/:story_id/vote/:vote", Votes, :story

    get "/submit", Stories, :new
    post "/submit", Stories, :create

    delete "/logout", Account, :logout
  end
end

See Mama, no Controller repeated over and over again.

However recently I am thinking about grouping the controller, view, and templates within single “domain-oriented” directory.

dimitarvp

dimitarvp

My rule of thumb for Elixir projects is: if your namespaces have 4 levels then it’s probably time to break apart your app.

As all such rules there are exceptions, obviously, but us the humans can only keep so much complexity in our heads.


BTW I never liked the “controller” moniker anyway. It’s IMO a misnomer and it would be more accurate as “HTTP inbound sink” (or something more brief than that; my English vocabulary isn’t great). The hell does it “control”?

If it were up to me I’d like have files like these:

  • lib/myapp_web/inbound/users_server.ex
  • lib/myapp/orders/cart.ex
  • lib/myapp_web/renderers/line_item_viewer.ex

Or some such.

Where Next?

Popular in Discussions Top

cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
laiboonh
Hi all, I am trying to convince my team to use liveview over the current react. What are some of the points where one should consider us...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
jesse
Hi everyone, I hesitated to post this here because I don’t want you to think I’m spamming, but I’ve been working on a Platform-as-a-Serv...
New
Owens
Hello all, I am developing a new mobile app with Flutter frontend and Phoenix backend. The mobile app has real-time task management and c...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

Other popular topics Top

New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
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 52341 488
New
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

We're in Beta

About us Mission Statement