Organization of code in Phoenix application

I’ve been working on porting an fairly complex API I have over to Elixir and Phoenix. Since it’s more complicated than just some CRUD controllers, I’ve felt like the default structure of the Phoenix application doesn’t really work. There needs to be a service layer that handles more complex business logic.

Most of the larger applications I’ve built have something like this that sits on top of the domain layer, and the controller layer interacts with it (and often the domain layer, too). But I don’t typically consider this layer of the application part of the “web” layer since it really has nothing to do with web, meaning it should go in the “lib” folder. However, since it interacts with the model layer, it’s strange to me to have the model layer in the web folder and the service layer in the lib folder.

Which made me wonder why the “models” folder is in the web folder anyway. This seems to only make sense for a very basic web application with just some controllers on top of the model. From what I can tell the only part of the web code that gets used in the model layer is the macro in the web.ex file that just aliases some Ecto libraries.

Is there any reason I can’t move this folder to the lib folder. Then the web folder would just be controllers, views, channels, and some static content. Which makes sense for a web layer.

Just curious how other people have approached this issue.

1 Like

As of Phoenix 1.2, you can move everything to the lib directory. Previously, only the web directory would be compiled on change, however this is no longer the case.

You may find this PR of interest https://github.com/phoenixframework/phoenix/pull/1871

5 Likes

That is an interesting discussion. Glad to know the direction they are headed!

Thanks!

1 Like