Request, Response
A Phoenix app can basically be boiled down to a function that receives a HTTP request, and returns a response. We'll begin with this premise, and start to understand the important parts involved in this process.
-
Request, ResponseWelcome & Setup
We'll set our sights on some specific goals for our Phoenix learning adventure, and ensure that everyone has what they need to get the most out of the training.
-
Request, ResponseEndpoint & Routing
Requests enter your app through an Endpoint, which your app usually have just one. We'll look at this chain of Elixir Plugs, which ends at the Router, the module ultimately responsible for delegating request-handling to an appropriate Controller.
-
Request, ResponsePlugs & Pipelines
Plugs are at the core of Phoenix and a relatively simple concept: plugs accept a connection as an argument, and return a slightly-modified connection.
Once we chain a few plugs together, it's easy to see how basic building blocks assemble into a complete application.
-
Request, ResponseEXERCISE: Routing to the Pages controller
Your new Phoenix project comes with a
PagesController
that renders a landing page you can see at localhost:4000. See if you can infer from how files are organized in the templates folder, how you can make a static HTML page reachable at ` localhost:4000/my_page. -
Request, ResponseEXERCISE: Hating on a Content-Type
Apparently SOAP is out of style. Let's return a discriminatory error message if we receive any request that has a SOAP content type.
SOAP is for washing up, not for APIs.
Build a Plug that interrupts the pipeline (returning a HTTP error for an incoming request) if we ever receive a request for a SOAP XML document (
Content-Type: application/soap+xml
) -
Request, ResponseThe Controller Responds
Now that we understand how to leverage Phoenix's routing layer, let's take a closer look at Controllers -- the modules ultimately responsible for responding to a request.
-
Request, ResponseEXERCISE: Marco, Polo
In the PagesController, create a new action that returns a welcome message for a name. The router should delegate responsibility to this action for GET requests to
http://localhost:4000/welcome_me/<name>
andhttp://localhost:4000/welcome_me?name=<name>
.If the
Accept
header for the incoming request indicates that the client wants HTML, they should receive a reasonable HTML response, otherwise, they should receive JSON. -
Request, ResponseViews & Templates
In contrast to other web frameworks, Phoenix's view layer is exceedingly easy to understand and use. Judging by how easy it is to keep views simple, performant, and easy to manage, It's clear that the hard-learned lessons from older frameworks have paid off.
-
Request, ResponseEXERCISE: Revise our HTML response
Let's revise our previous approach to HTML rendering for our welcome endpoints, so that they take advantage of Phoenix's view layer. Make sure to use assigns to make values available to views.
-
Request, ResponseLunch
Break for Lunch