1. Intro to Contexts

View Source

Phoenix guides are broken into several major sections. The main building blocks are outlined under the "Core Concepts" section, where we explored the request life-cycle, wired up controller actions through our routers, and learned how Ecto allows data to be validated and persisted. Now it's time to tie it all together by writing web-facing features that interact with our greater Elixir application.

When building a Phoenix project, we are first and foremost building an Elixir application. Phoenix's job is to provide a web interface into our Elixir application. Naturally, we compose our applications with modules and functions, but we often assign specific responsibilities to certain modules and give them names: such as controllers, routers, and live views.

However, the most important part of your web application is often where we encapsulate data access and data validation. We call these modules contexts. They often talk to a database, using Ecto, or APIs, using an HTTP client such as Req. By giving modules that expose and group related data the name contexts, we help developers identify these patterns and talk about them. At the end of the day, contexts are just modules, as are your controllers, views, etc.

Contexts are not a new concept either. For example, anytime you call Elixir's standard library, be it Logger.info/1 or Stream.map/2, they are the entrypoint to rich functionality, often implemented across multiple modules which are hidden from us. Contexts in Phoenix are the same: plain modules that act as boundaries to decouple and isolate parts of your application.

Let's use these ideas to build out our web application. Our goal is to build an ecommerce system where we can showcase products, allow users to add products to their cart, and complete their orders. Opposite to other Phoenix guides, these guides are meant to be read in order.

Our ecommerce application

Let's start an application from scratch to build our ecommerce, using Phoenix Express. We will call the application hello.

For macOS/Ubuntu:

$ curl https://new.phoenixframework.org/hello | sh

For Windows PowerShell:

curl.exe -fsSO https://new.phoenixframework.org/hello.bat; .\hello.bat

If those commands do not work, see the Installation Guide and then run mix phx.new:

$ mix phx.new hello

Follow any of the steps printed on the screen and open up the generated hello project in your editor.

We are ready to move to the next chapter.