Understanding Phoenix when you know (a bit of) Elixir?

Hi,

I have been learning Elixir and OTP (then also some Erlang as it is in fact mandatory for using both) for several monthes.

I made a small project using them (“a (very) Simple SFTP Client/Server in Elixir” ) which seems to be at least operational.

If my code is surely far from beeing “pure and clean” for Elixir buffs like you, I would like to believe that, after some epic struggles with a lot of books and videos and thanks to some help in this forum, I managed to roughly understand the main principles of the language and OTP.

My next plan is now to learn Phoenix. And then LiveView…

In this goal, I bought the “Programming Phoenix >= 1.4” book (which seems to be the only up-to-date book about the current Phoenix version) and begun to follow the “Rumbl” application exposed as an example/exercice.

But now that I’m at the first third of the book, I’m a bit confused by the massive number of files scattered in so many odd categories such as contexts, controllers, templates, views et al.
And then the questions : how and where should I set my code in this huge tree ? What precisely are these concepts and how do they articulate whith each others ?

So if you could advice some (up-to-date) resources (books, videos, web sites) which could - clearly- explain that for a guy who knows Elixir but who is not familiar with others full stack frameworks such as Ruby On Rails (which seems to be the starting point for Phoenix)… ?

Thanks in advance.

1 Like

You need to learn about MVC. Obviously there are no models in Phoenix, but that would be Schema and Context.

Most of the time You will put your code in contexts.

1 Like

The rough determination is the following:

Context at their core are nothing more than what you did before: Put business logic into modules. This is mostly something special in the context of webframeworks, because people tent to not separate this from the actual code handling specifically webrequests.

Now the code in context cannot yet be called via web requests. This is what your endpoint/router/controllers are for. They receive requests and handle them in various ways. The endpoint is for stuff affecting any requests coming in, the router handles forwarding to controllers based on things like paths or other determinations, while controllers/their actions are there to handle the specific task the request is trying to affect. This could be requesting e.g. a list of users as well as submitting changes. The controller itself should at best only handle logic, which is specific to a http request, like reading params or headers or stuff like that and use functions of your context modules to handle the actual business logic. It’s like a translation from http request to function call of context modules. A good comparison would be a CLI controller, which translates argv input into actual function calls.

The second part of the controller is to respond to the request with data formatted in json/html/…. So the returned values of the context function calls need to be converted. This task is usually delegated to the view layer. The view layer is responsible for converting the retrieved data into the proper binary (or textual) representation for the http response. For markup languages it’s often quite strange to work with elixir code to build the markup, therefore view layers come with templating capabilities, which allow you to mix static markup with dynamic code parts. But e.g. for json data you usually don’t use templates, but you’ll prepare the structure in a native datatype and just convert it at once to a json string. Again the comparison would be a CLI controller calling into an extracted module to convert data into a table for shell output, because you don’t want each controller to implement the table formatting again.

2 Likes

I wanted to write a nice post but @LostKobrakai was quicker :smiley: also since you’ve never used any Full Stack framework @kokolegorille has also a good point, in that case I can recommend this page https://roadmap.sh/backend Because elixir & phoenix have an awesome documentation I will go with that after you gather knowledge what exactly is an Full Stack framework and what are its main parts (MVC, REST etc…)
Keep going, everything takes some time :slight_smile:

1 Like

I forgot to mention that I’m currently a javascript frontend developper (using Vue.js + Vuetify): that’s perhaps what made me interested in LiveView ? :wink:
Your (nice) advices lead me to then reformulate my question:
Do you know some resources (video courses, books or others) which explain in details the “main parts” of a “generic” FS Framework, to go deeper on the main lines given by @LostKobrakai ?