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

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement