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
@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 ![]()
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
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.exlib/myapp/orders/cart.exlib/myapp_web/renderers/line_item_viewer.ex
Or some such.
Popular in Discussions
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








