Jeffrey Hicks

Jeffrey Hicks

Platform Eng @R360

06 - Phoenix crash course #6: Verified routes (~p sigil)

Learn how to use Phoenix's verified routes with the ~p sigil for compile-time route validation

By Daniel Bergholz • Aug 19, 2025

Learn how to use Phoenix’s verified routes with the ~p sigil for compile-time route validation.

What are verified routes?

  • A Phoenix feature that checks route validity at compile time, reducing broken links in your app
  • Uses the ~p sigil (e.g., ~p"/products") for route validation and path generation
  • Works in controllers, views, and live views—auto-imported through using ShopWeb macros

How does it help?

  • Catches typos and invalid paths at compile time, not runtime
  • Gives warnings/errors if a path doesn’t exist in your router

Convenient features

  • Query params: Easily add query parameters to links with interpolation:
    • ~p"/?console=#{console}&admin=true" or ~p"/?#{[console: "Xbox", admin: true]}"
  • Controller usage: Use redirect(conn, to: ~p"/products") for safe, error-checked redirects
  • URL helper: Get the full URL (including hostname/protocol) with the url() function and the ~p path: url(~p"/products")

Example

  • If you misspell a route in a link or redirect, compilation fails or IDE warns you
  • Passing dynamic or static parameters is easy and safe

Bottom line

Always use ~p (verified route sigil) instead of hardcoded path strings for links and redirects. Guarantees valid routing and safer code in Phoenix apps.

Related

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