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].

First Post!

dimitarvp

dimitarvp

Well, people use alias a lot and having e.g. Users is going to be confusing, especially having in mind that Phoenix encourages you to have context modules with the model name in plural. So what would Users mean? The context or the controller?

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.

Last Post!

cevado

cevado

Sorry, but I’d say untenable is a strong word for this.
Most modern editors are configurable enough for the “tab” problem to be dealt with. Just looking for <editor> full path tab gives me:

for the “alias” problem a simple change from UserController to Controllers.User, UserView to Views.User would do the trick, and it’s just 2 characters added. I think it even makes it easier to alias in most use cases.

Where Next?

Popular in Discussions Top

AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
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
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