Search documentation of phoenix_kit

# PhoenixKit.Sitemap.RouteResolver (phoenix_kit v1.6.19)

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

extract_prefix(pattern)

Extracts URL prefix from a route pattern.

find_content_route(type, name \\ nil)

Finds route pattern by content type.

find_index_route(arg1, entity_name)

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

find_route(plug_module, opts \\ [])

Finds path for a specific plug module.

get_router()

Gets router module with automatic discovery.

get_routes()

Returns all routes from the parent router.

# Functions

# 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")
- `:entity` - Finds routes matching entity name (singular or plural form)

## Examples

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

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

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

# find_index_route(arg1, entity_name)

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

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

## Examples

    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.