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_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?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Most Liked- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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:
and then in
my_app_web:See the Phoenix.View docs.
APB9785
The templates folder is defined in
lib/your_app_web.ex(look fordef view do)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 likeSee match/5 for options accepted. (resources/get/post/put/… calls match/5)
Last Post!
7rans
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!