Jeffrey Hicks

Jeffrey Hicks

Platform Eng @R360

03 - Phoenix crash course #3: Request life-cycle

Complete walk-through of the Phoenix HTTP request life-cycle from endpoint to response

By Daniel Bergholz • Aug 19, 2025

Complete walk-through of the Phoenix HTTP request life-cycle from endpoint to response.

Flow of a request

  1. Endpoint: Receives HTTP request, runs “plugs” (middleware)
  2. Router: Maps the request to a controller/action by URL and verb
  3. Controller: Defines actions (e.g., index for listing products, show for a specific product)
    • Access request info via conn (connection) and params (URL/query)
    • Actions usually query data (not shown yet—static for demo) and call render
  4. View:
    • Handles presentation logic
    • Convention: [Controller]HTML module, returns HTML using the HEEx (HTML + Elixir) syntax
    • Can use embed_templates/1 to keep large templates in separate files
  5. Hot code reload: Changes to templates auto-refresh in dev
  6. Dynamic routes:
    • /products = index view
    • /products/:id = show view (gets product ID from params)
    • Assigns props to views (assigns), accessed with @prop in HEEx
  7. Rendering JSON: Just create [Controller]JSON and return maps; Phoenix converts to JSON (will be covered in detail later)
  8. Contexts (teaser):
    • To come: business logic/data access is kept outside controllers, in “contexts,” for clean separation

Key takeaway

This video demystifies how an HTTP request is routed through Phoenix to return HTML (or JSON), with a concrete example building product listing/detail routes and views, highlighting core concepts like routing, controllers, views, assigns, and development workflow.

Related

#phoenix-and-elixir #phoenix-crash-course #daniel-bergholz