Jaypee

Jaypee

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.

Most Liked

LostKobrakai

LostKobrakai

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.

kokolegorille

kokolegorille

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.

anon73

anon73

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 Backend Developer Roadmap: What is Backend Development 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:

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement