cevado

cevado

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

Showing Posts 1 to 10

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?

al2o3cr

al2o3cr

Beyond the aliasing issue @dimitarvp mentions, the bigger concern IMO is the “tab problem”.

In short, having many files with similar / identical names can cause confusion when users have an editor that only displays the base name of the file as a label. In your example, is a tab labeled resource.ex the controller or the corresponding schema?

Rust had a similar (but bigger) situation a few years back; the “default” place to put code for a module foo was in foo/mod.rs - so if you had many crates open, ALL the tabs would be named mod.rs :scream: The thread of discussion is long and tangled, but you can start from the final RFC that changed the approach so that foo.rs would work.

tfwright

tfwright

Wouldn’t the easy solve there to use the :as option? Although as I type that I realize my approach in Elixir apps that have this problem is to just alias up to the namespace so I can reference X.Users and Y.Users in any module that needs to reference both.

dimitarvp

dimitarvp

Indeed but then why rename in the first place? You’d save writing something like 5-6 characters at best, at the cost of using unnecessary renaming in other form (namely the :as option).

tfwright

tfwright

Good point. It seems like the as option only really helps with the NS immediately above the mod isn’t enough to distinguish it but something higher up is. But I’ve never worked on a a system where that was true.

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.

cevado

cevado OP

@dimitarvp but why someone would reference a controller outside of a router or the actual controller test?

@al2o3cr isn’t that already a problem with the default elixir naming standard? why having two standards would solve that?

I think the main objective should be consistency. unfortunately, the web folder is an alien thing to the rest of the elixir code of a project.

dimitarvp

dimitarvp

They very likely wouldn’t but aliasing things is a fact of life and IMO somebody is bound to get confused eventually. I wouldn’t risk it.

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.

cevado

cevado OP

:heart_eyes:
i’ve been doing that too. i’d love to see it as a standard in the framework, and I say that because generators usually direct people to a standard solution. i feel so weird on the projects that I’ve worked that are the necessity of custom plugs and people created and named plugs according to the “phoenix standard”.

personally I only use generators for migrations and everything else I create by hand so it doesn’t impact me that much. one other thing that I usually change is the folder names too:

lib
├── web.ex
├── my_app.ex #application module
├── core
│   ├── repo.ex
│   ├── ...
├── web
│   ├── controllers
│   │    ├── ...
│   ├── templates
│   │    ├── ...
│   ├── views
│   │    ├── ...

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews