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_controller → foo_web/bar/controller, and I tried to change the module name accordingly – FooWeb.BarController → FooWeb.Bar.Controller. But this broke all my Routes helpers. 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?
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:
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
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.