onelixir
The Phoenix Framework Doc shows the Directory Structure like this:
lib/hello_web
├── controllers
│ ├── page_controller.ex
│ ├── page_html.ex
│ ├── error_html.ex
│ ├── error_json.ex
│ └── page_html
│ └── home.html.heex
├── components
│ ├── core_components.ex
│ ├── layouts.ex
│ └── layouts
│ ├── app.html.heex
│ └── root.html.heex
├── endpoint.ex
├── gettext.ex
├── router.ex
└── telemetry.ex
I have some question about this:
- Now we have just one
pagecontroller, insidecontrollersdirectory we have 2page_*files and 1page_*directory, if we also add JSON support, then one more file, one controller = 3 files + 1 directory, if we have 10 controllers, will we have 30 files + 10 directories incontrollersdirectory? - If we also need email, like in Rails, where should we put its directory, do we have some generator? And how about jobs?
I ask these questions because I just come from Rails, Phoenix looks simple but also brought too much confuse.
I truely hope Phoenix world have a book like 《 Agile Web Development with Rails 7》, The official document is good, but lack of focus and best practices.
Trending in Discussions
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...
New
The obligatory hello world thread!
Who are you and where are you from? :stuck_out_tongue:
New
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
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using.
We’re particularly inte...
New
Is there a word for the ~> symbol used in Version strings?
Do you also just call it a Squiggle Arrow™ ?!
New
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
AcmeScript — Writing JS hooks as if I were still using Elixir
I’ve been having fun building a little something over the last few days: Ac...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sodapopcan
Phoenix is not opinionated like Rails. There is a tiny bit of “magic” happening with via some dabs of reflection but it’s not pervasive. Phoenix doesn’t care at all where you put your files and the generated structure is more of a guideline. In fact, Phoenix is arguably closer to Sinatra than it is to Rails with
$ mix phx.newbeing what makes it a full fullstack framework. There are single file examples of Phoenix apps out there!I also came from Rails so I definitely shared some of your pain. I was particularly perturbed that many examples don’t show you exactly were to put files. I’m not saying this is better or worse, but you get used to it pretty quickly.
Your example is actually slightly flawed when comparing number of files to Rails. In Rails, the equivalent
page_files you mentioned would becontrollers/page_controller.rbandviews/pages/show.html.erbandhelpers/page_helper.rb, so actually 3 files and 3 directories! The_htmlfiles in Phoenix are a new concept but basically they used to be called views. Phoenix splits up the concept of “view” into “html” and “template” (it used to be “view” and “template”) where html compiles the templates and controllers render the html. This is actually closer to how the general concept of a view quote-unquote “should” work (the “view” is the presentation logic whereas the “template” is the static markup) whereas Rails simplifies it by combining view+template then optionally allowing a ‘helper’ file if you need added presentation logic. In Phoenix, you would put your “helpers” in thePageHTMLmodule. So, you are right in that Phoenix always forces you to have three modules (er, two modules + one template). If you’re strictly concerned about number of files and you don’t mind putting your templates in functions, you can actually define your templates right inPageHTMLthen you don’t need theshow.html.heexfile:I actually don’t use the default directory structure at all for web. I don’t have many controllers as I often have LiveViews but I break up my
Webmodule into site sections, so I don’t have acontrollersorlivedirectory. Mine would look like this:You don’t have to do anything special to make this work—just move the files around. Although you’ll have to likely have to change the glob given to
PageHTML.embed_templates.onelixir
Thank you so much for your explanation, I feel much clearer now. Yes, this is really what makes me feel confuse. In rails, if you don’t put things in the right place, maybe it won’t work.
In other side, you are also right that Rails has 3 directories + 3 files, It’s just seems Rails put them in a “comfortable” place, but in Phoenix we are all in “controllers”, I just cannot imagine what will it looks like if it were a large app. That’s why.
sodapopcan
The reason it’s nested under controllers is that it mimics the shape of what is going on. It’s a more uniform, single-direction relationship of controller → html → template whereas in Rails it’s controller → view with a helper on the side (and I’m willing to be you’ve worked on Rails projects that abuse
helper_methodto pull helper methods into the controller as well). It arguably encourages a cleaner design as your app grows. You could even make it look like Rails if you wanted:I would not recommend this, though
Personally, I wouldn’t be upset if Phoenix were more opinionated. It certainly helps with learning quicker. My biggest hurdle at the time I learned Rails was organization so Rails was a godsend! However, I worked on a massive Rails app (well over 300 models if that is any kind of measure) and organization becomes very hard there. Decisions were made and a good solution was found, but you end up fighting the framework in that case. With Phoenix there would be far less friction when it comes to re-organizing a massive app like that. Using ActiveRecord in the RailsWay™ actually became a problem at that scale as well… turns out having 300
although that is getting off topic.
has_one/has_manys in theUsermodel wasn’t too scalableEDIT: Oops, I meant
includehelpers into the controller! I gothelper_methodbackwardsonelixir
I can imagine what it will looks like if 300 models in a Rails project, or 300
has_manyin theUsermodel. I worked on a Rails project which has 20+has_manyand I already feel my brain not enough.But can you a bit explain why with Phoenix there would be far less friction?
sodapopcan
It’s really just because the framework isn’t concerned about where your files are. For example, our Rails app was split up by feature so we ended up with many directories that essentially each had a mini Rails apps in them. It’s not the biggest deal in to make this work but you’re still going against the framework conventions. You also lose the social friction of new people coming into the app expecting it to look like a Rails app and it doesn’t. Phoenix doesn’t set up any such expectation. I’m not really saying it’s a massive, life-changing improvement but it is part of what drew me to Phoenix even if I do miss some stuff about Rails.
As far as Ecto vs ActiveRecord goes, I would say that is a massive improvement. It takes a bit to get comfortable with the shift away from an ORM, but Ecto is essentially like a super powerful and better designed
arel. You don’t run into the same scaling issues with it since it just returns structs (which are no heavier than bare maps) as opposed to massive objects with methods spanning at least five different concerns.onelixir
Thank you so much again, I think I learn a lot from your answers, I will dive into Phoenix to feel the feeling you feel.
Also, one of the most happy thing in Elixir land is because we have so kindly people like you