PhoenixKit.Modules.Sitemap.RouteResolver (phoenix_kit v1.7.71)

Copy Markdown View Source

Resolves actual routes from parent application router.

Uses router introspection to automatically detect URL patterns, falling back to Settings configuration when introspection fails.

Resolution Priority

  1. Router Introspection - automatic detection from parent app router
  2. Settings override - manual configuration in PhoenixKit Settings
  3. Hardcoded fallback - default values

Usage

# Find path for specific plug module
RouteResolver.find_route(PhoenixKitWeb.Users.Registration)
# => "/users/register"

# Find content route by type
RouteResolver.find_content_route(:pages)
# => "/pages/:slug"

RouteResolver.find_content_route(:entity, "product")
# => "/products/:slug"

Summary

Functions

Checks if a content route (posts, entities, etc.) requires authentication.

Extracts URL prefix from a route pattern.

Finds route pattern by content type.

Finds index route for content type (list page without :slug).

Finds path for a specific plug module.

Gets router module with automatic discovery.

Returns all routes from the parent router.

Checks if a route requires authentication based on its on_mount hooks.

Functions

content_route_requires_auth?(type, name \\ nil)

@spec content_route_requires_auth?(atom(), String.t() | nil) :: boolean()

Checks if a content route (posts, entities, etc.) requires authentication.

Examples

content_route_requires_auth?(:posts)
# => false

content_route_requires_auth?(:entity, "product")
# => false

extract_prefix(pattern)

@spec extract_prefix(String.t() | nil) :: String.t() | nil

Extracts URL prefix from a route pattern.

Examples

extract_prefix("/pages/:slug")
# => "/pages"

extract_prefix("/content/*path")
# => "/content"

extract_prefix("/blog/posts/:id")
# => "/blog/posts"

find_content_route(type, name \\ nil)

@spec find_content_route(atom(), String.t() | nil) :: String.t() | nil

Finds route pattern by content type.

Types

  • :pages - Finds routes that look like page routes (contain :slug and plug name contains "page")
  • :posts - Finds routes that look like post routes (contain :slug and plug name contains "post")
  • :entity - Finds routes matching entity name (singular or plural form)

Examples

find_content_route(:pages)
# => "/pages/:slug"

find_content_route(:posts)
# => "/posts/:slug"

find_content_route(:entity, "product")
# => "/products/:slug"

find_content_route(:entity, "page")
# => "/pages/:slug"

find_index_route(type, name \\ nil)

@spec find_index_route(atom(), String.t() | nil) :: String.t() | nil

Finds index route for content type (list page without :slug).

Examples

find_index_route(:posts)
# => "/posts"

find_index_route(:entity, "page")
# => "/page" or "/pages"

find_index_route(:entity, "product")
# => "/products"

find_route(plug_module, opts \\ [])

@spec find_route(
  module(),
  keyword()
) :: String.t() | nil

Finds path for a specific plug module.

Options

  • :verb - HTTP verb to match (default: :get)

Examples

find_route(PhoenixKitWeb.Users.Registration)
# => "/users/register"

find_route(MyApp.SomeController, verb: :post)
# => "/some/path"

get_router()

Gets router module with automatic discovery.

Resolution order:

  1. config :phoenix_kit, router: MyAppWeb.Router
  2. Via endpoint from config :phoenix_kit, endpoint: MyAppWeb.Endpoint
  3. Auto-discover from OTP applications (finds *Web.Router modules)

get_routes()

@spec get_routes() :: [map()]

Returns all routes from the parent router.

Returns empty list if router is not available.

route_requires_auth?(route)

@spec route_requires_auth?(map() | String.t()) :: boolean()

Checks if a route requires authentication based on its on_mount hooks.

Returns true if the route uses authentication-requiring on_mount hooks:

  • :phoenix_kit_ensure_authenticated_scope
  • :phoenix_kit_ensure_admin

Examples

route_requires_auth?(%{metadata: %{...}})
# => true/false

# Check by path pattern
route_requires_auth?("/posts")
# => true/false