alaadahmed

alaadahmed

Organise files under *controllers* folder similar to *live* and context folder

Hi folks,
I tried Phoenix 1.7 and it is awesome, but I have small suggestion, which in my opinion will organize files in a better way.
So when I use phx.gen.html to create a controller and its modal and html counter parts like so:

mix phx.gen.html Academy Student students name email age

This will create:

app
|-- academy/
|-- academy.ex

app_web
├── controllers
│   ├── error_html.ex
│   ├── error_json.ex
│   ├── page_controller.ex
│   ├── page_html/
│   ├── page_html.ex
│   ├── student_controller.ex
│   ├── student_html/
│   └── student_html.ex     

Then I used again phx.gen.html to create one more controller and its modal and html

mix phx.gen.html Academy Teacher teachers name email subject

So now this will add to controllers folder some other files

app_web
├── controllers
│   ├── error_html.ex
│   ├── error_json.ex
│   ├── page_controller.ex
│   ├── page_html/
│   ├── page_html.ex
│   ├── student_controller.ex
│   ├── student_html/
│   ├── student_html.ex
│   ├── teacher_controller.ex
│   ├── teacher_html/
│   └── teacher_html.ex       

Now you see where are going, suppose this will be large app, and has 50 controllers, we will find in this folders a lot of files and folder and will be confusing specially if we will create _json.ex part!
My suggestion is, why this couldn’t be like in app folder or live folder? I mean each controller in its own folder, containing all related files and folder together, to match with live at least like so:

app_web
├── controllers
│   ├── error_html.ex
│   ├── error_json.ex
│   ├── page
│   │   ├── page_controller.ex
│   │   ├── page_html
│   │   └── page_html.ex
│   ├── student
│   │   ├── student_controller.ex
│   │   ├── student_html
│   │   └── student_html.ex
│   └── teacher
│       ├── teacher_controller.ex
│       ├── teacher_html
│       └── teacher_html.ex

This will match with live folder, as every live we create using mix phx.gen.live usually has its own folder under live.

I hope I made it clear.

Most Liked

josevalim

josevalim

Creator of Elixir

Your approach is fine and I might personally even prefer it, and Phoenix will allow you to do both. But speaking from experience, whenever the module structure did not reflect the file structure in Phoenix, people would strongly dislike that too. So some people would expect your code to define Page.PageController, Page.PageHTML and so on, which is of course too verbose. And defining only Page.Controller leads to other sorts of ambiguity (imagine trying to a find a given Controller alias).

In other words, you can/should go for it. But I suspect if we had gone with it, we would have folks asking to change it to the current one. YMMV. :slight_smile:

sodapopcan

sodapopcan

As someone who is not a particularly big fan of colocated files I like this as it’s more “package-y” (which I do like).

I guess the more classic Elixir-y way would be like:

app_web
├── controllers
│   ├── page_controller
│   │   ├── page_html
│   │   └── page_html.ex
│   └── page_controller.ex

For what it’s worth—since this is something that generally gets to me especially since it feels like there is a bit of a push towards it—I really dislike this manner of colocation because I only ever use tree browsers to get a sense of the structure. When I see the flat colocation structure, it’s incredibly noisy (which I believe is what you’re getting).

This is because if I think of an application general tree structure (talking module tree, not file tree), controllers are an entry point then drill down into their views. I think we’re past the world of “shared views” because we now have components so there is no reason to want to see a giant list of views.

To go further down the bike sheddy rabbit hole, I really like the “classic” pattern of foo.ex colocated with a foo/ directory. I’ve worked with people and seen recent projects that don’t do this, and I understand the distaste (digging into Rust lately I’ve seen cargo programmatically allows both styles) but when it comes to glancing a file tree, it’s really nice seen bare files as entry points and then if there are supporting files, I know that because there is a related directory. And then if I see a directory with no bare .ex file, well, I don’t know what that is off the bat but I know it’s something to be considered different.

Anyway, that last paragraph was super bike-sheddy but I just appreciate your post.

alaadahmed

alaadahmed

Ya, I do also like what you proposed, Elixir-y way.

Where Next?

Popular in Proposals: Ideas Top

tubedude
Hey, Earlier i posted a question, but after some research I think a proposal is due. Working on a Phoenix Live View app, I needed clien...
New
MUSTDOS
Hello all! assign(socket, :name, “Elixir”) Why can’t we have assign/stream take a group of atoms and maps/structs as a default to r...
New
pelopo
Hi, Oficial docs got this wonderful feature to download the ePub version that we can chuck to our kindles and read it. Now we are in the...
New
manhvu
In a large repo, working with module need to add alias too much is quite annoyed and not good for organizing code. I think better add su...
New
shahryarjb
When proposing or suggesting something please consider: As my experience implementing getBoundingClientRect as Phoenix.LiveView.JS funct...
New
ffloyd
The Problem Currently, if I define a struct in the following way: defmodule MyStruct do # Both x and y will have the FIXED values unti...
New
Astolfo
When generating a release with phx.gen.release --docker a debian version is used by default to avoid “DNS issues” on Alpine. It seems li...
New
bamorim
Story behind Recently, I gave a talk on a meetup about improving performance of Phoenix applications and the example app was a LiveView ...
New
nunobernardes99
On 1.8+, when we generate an authentication system with mix phx.gen.auth we can make use of magic link login which is amazing and a great...
New
MatinDevs
Currently, there are ongoing discussions about enhancing Phoenix LiveView, particularly focusing on improving performance and user experi...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47849 226
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement