Jeffrey Hicks

Jeffrey Hicks

Platform Eng @R360

07 - Phoenix crash course #7: Routing

Learn Phoenix routing fundamentals including resourceful routes, nested resources, scopes, and pipelines

By Daniel Bergholz • Aug 19, 2025

Learn Phoenix routing fundamentals including resourceful routes, nested resources, scopes, and pipelines.

Viewing and managing routes

  • Find all routes in router.ex or by running mix phx.routes
  • Use terminal tools (grep) to filter routes by resource (e.g., products)

Resourceful routing

  • Use resources "/products", ProductController to auto-generate all CRUD endpoints (GET, POST, PUT/PATCH, DELETE)
  • Limit routes with only: [:index, :show] or exclude with except: [:delete]

Nested resources

  • Supports nesting (e.g., blog posts under users):
    resources "/users", UserController do
      resources "/posts", PostController
    end

Scopes

  • Organize routes by path prefix (e.g., /api, /dashboard) and run different pipelines
  • Example:
    • UI routes under /
    • API routes under /api
    • Admin/dashboard under /dashboard

Pipelines

  • Collections of plugs (middleware) that run in order, e.g., the :browser pipeline for standard web pages, or custom ones like :auth for authentication
  • Attach one or several pipelines to scopes (e.g., pipe_through [:browser, :auth])

Tips

  • You can halt request processing in a plug (e.g., for security/redirects)
  • Prefer explicit, readable route definitions—only use resources if you need most CRUD routes

Summary

This lesson covers Phoenix router basics, resourceful/nested routes, scopes, and pipelines for organizing endpoint behavior and authentication.

Related

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