7rans

7rans

I’m not too keen on the way Phoenix lays out its directory structure – for my purposes at least. All the files are so spread out, being organized by file types (e.g. controllers, views, templates, etc.) instead of grouping them together by a particular “context” (not necessarily to be confused with Phoenix’s idea of “context” for business logic code).

So I tried to rearrange things a bit, and so far it works, but with some issues. I moved my controllers and views into contextual sub-directories, eg. foo_web/controllers/bar_controllerfoo_web/bar/controller, and I tried to change the module name accordingly – FooWeb.BarControllerFooWeb.Bar.Controller. But this broke all my Routes helpers. :frowning: Ironically I don’t much care about that as I think generating all those helper functions is overkill and bad design. They aren’t necessary and could easily be replaced by a few functions (e.g. path() and url()) in which one just passes the root route, or something like that. But I am worried other things may be broken b/c of the changes I made that I am not yet aware and haven’t encounter yet. Is there anything else my change might break? Or are the Routes helpers the only issue?

Lastly, I would like to also move my templates into the contextual directories and out of templates, but I am not sure how to tell Phoenix where to find them if I make that change. Is that possible?

Showing Posts 1 to 5

APB9785

APB9785

Creator of ECSx

The templates folder is defined in lib/your_app_web.ex (look for def view do)

joaoevangelista

joaoevangelista

I think you need to set a name for them since now they are all called controller_path. on your route you can do it like

scope "/", FooWeb do
  resources "/bars", Bar.Controller, as: :bar # will name as bar_path
end

See match/5 for options accepted. (resources/get/post/put/… calls match/5)

stefanchrobot

stefanchrobot

Phoenix compiles the templates into the view code, so it needs to find them at compile time. You can dynamically render any view from a controller, but in most cases you’ll have a dedicated one per controller, so to avoid the boilerplate there is a naming convention for linking the controller with the view. This implies how route helpers infer the function names.

I think you should be able to mostly configure things the way you want.

I personally group the code by feature but keep the naming scheme, which requires minimal config changes:

- lib
  - my_app_web
    - password_reset
      - password_reset_controller.ex (MyAppWeb.PasswordResetController)
      - password_reset_view (MyAppWeb.PasswordResetView)
      - new.html.eex

and then in my_app_web:

defmodule MyAppWeb do
  def view do
    quote do
      use Phoenix.View,
        root: "lib/my_app_web",
        # instead of root "lib/my_app_web/templates"
        namespace: MyAppWeb
  end
end

See the Phoenix.View docs.

7rans

7rans OP

Nice! Thank you. That did the trick for the routes.

I’m very much liking my naming scheme better (despite the additional as code). At the very least it just reads cleaner (to me). It makes me think how nice to would be if I could drop the Controller part of the namespace and Phoenix knew to look for it by default. e.g. so in place of your example to be able to just write:

scope "/", FooWeb do
  resources "/bars", Bar
end

I did some additional searching and, I guess not surprisingly, others have restructured their project similarly. E.g. found this discussion from awhile back making it the default.

7rans

7rans OP

I was just about to do that this morning, but @joaoevangelista solution made it unnecessary – its a little extra boiler plate but works for me, and is good to know about in any case.

Thank you for the view code. That did the trick for the templates. My life is easier now. Yeah! :smiley:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
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
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
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews