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

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
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
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New
AstonJ
This might be a bit disturbing for some but it’s happening - computers running on living human neurons. They’ve made them smart enough t...
New

Other Trending Topics Top

marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews