MyApp.Controllers.User instead of MyApp.UserController

Dears, I would like to use custom names in my controllers and views, following the same patterns as in Ecto applications. For example:

defmodule MyApp.Controllers.User [/my_app_web/controllers/user.ex]
defmodule MyApp.Views.User        [/my_app_web/views/user.ex]

The actual pattern is a little bit noise, when you have more than 20 files

defmodule MyApp.UserController    [/my_app_web/controllers/user_controller.ex]
defmodule MyApp.UserView           [/my_app_web/views/user_view.ex]

Is there a way to customize it ? Meanwhile I tried, and notices that the routing, conn.private, etc… were not targeting the correct view, and it was not rendering.

thanks in advance !
Henry

https://hexdocs.pm/phoenix/Phoenix.Controller.html#put_view/2 needs to be used in the controller to set the view, automatic inferencing only works when using the Phoenix style naming conventions. Other than that it should all basically just work.

1 Like

One thing to consider is that generally this kind of naming makes it somewhat more difficult to jump around to files in editors and makes aliasing modules harder since doing something ambiguous is much easier.

2 Likes

thanks for the feedback Ben,
in fact, it worked with the put_view, keeping the Api.UserView module name, but with a clean filename. The folder structure became much more easy to follow.
using vim + ctrlp (fuzzy finder), I felt easy to find files also

├── api
│   ├── channels
│   │   └── user_socket.ex
│   ├── controllers
│   │   ├── fallback.ex
│   │   ├── session.ex
│   │   └── user.ex
│   ├── endpoint.ex
│   ├── gettext.ex
│   ├── plugs
│   │   └── auth.ex
│   ├── router.ex
│   └── views
│       ├── changeset.ex
│       ├── error.ex
│       ├── error_helpers.ex
│       └── user.ex
├── api.ex
├── core
│   ├── application.ex
│   ├── audit
│   ├── auth
│   │   └── token.ex
│   ├── identity
│   │   └── user.ex
│   ├── identity.ex
│   └── repo.ex
└── core.ex