| name | phoenix-guidelines |
| description | Provides Phoenix framework conventions and patterns. Use when working with Phoenix routers, scopes, routes, or dealing with Phoenix.View deprecation. |
Phoenix Framework Guidelines
This skill provides Phoenix framework conventions to help you write correct Phoenix applications.
Router Scopes and Aliases
Phoenix router scope blocks include an optional alias which is prefixed for all routes within the scope. Always be mindful of this when creating routes within a scope to avoid duplicate module prefixes.
Route Aliases
You never need to create your own alias for route definitions—the scope provides the alias automatically.
Example:
scope "/admin", AppWeb.Admin do
pipe_through :browser
live "/users", UserLive, :index
live "/posts", PostLive, :index
end
In the above example:
- The
UserLiveroute points to theAppWeb.Admin.UserLivemodule - The
PostLiveroute points to theAppWeb.Admin.PostLivemodule
Common mistake to avoid:
# DON'T DO THIS - redundant alias
scope "/admin", AppWeb.Admin do
pipe_through :browser
# UserLive already gets the AppWeb.Admin prefix from scope
live "/users", AppWeb.Admin.UserLive, :index # Redundant!
end
Phoenix.View Deprecation
Phoenix.Viewis no longer needed or included with Phoenix- Don't use it in new code
- Phoenix 1.7+ uses function components and
Phoenix.Componentinstead - If you see
Phoenix.Viewin legacy code, it should be migrated toPhoenix.Component
General Best Practices
- Use function components (
def my_component(assigns)) instead of class-based views - Leverage
Phoenix.Componentfor all template rendering - Use
Phoenix.VerifiedRoutesfor compile-time verified route helpers