Changing directory structure and module names

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?

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

2 Likes

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)

2 Likes

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.

7 Likes

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.

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:

1 Like