PhoenixKit.Utils.Routes (phoenix_kit v1.7.95)

Copy Markdown View Source

Utility functions for working with PhoenixKit routes and URLs.

This module provides helpers for constructing URLs with the correct PhoenixKit prefix configured in the application.

Summary

Functions

Returns a locale-prefixed admin path, bypassing the reserved-path locale stripping that path/2 applies.

Returns the path to the AI endpoints page.

Returns the default locale (base code) from the Languages module.

Returns a locale-aware path using locale from assigns.

Gets the base module name for the parent application.

Returns a full url with preconfigured prefix.

Returns the configured PhoenixKit URL prefix.

Functions

admin_path(url_path, locale)

Returns a locale-prefixed admin path, bypassing the reserved-path locale stripping that path/2 applies.

Admin routes use a /:locale/admin/* scope, so they need locale in the URL even though /admin is a reserved prefix.

Examples

iex> Routes.admin_path("/admin/users", "uk")
"/phoenix_kit/uk/admin/users"

iex> Routes.admin_path("/admin/users", nil)
"/phoenix_kit/admin/users"

ai_path()

Returns the path to the AI endpoints page.

get_default_admin_locale()

Returns the default locale (base code) from the Languages module.

Extracts the base code from the default language (e.g., "en-US" becomes "en"). Falls back to "en" if no default language is configured.

Examples

iex> Routes.get_default_admin_locale()
"en"

locale_aware_path(assigns, url_path)

Returns a locale-aware path using locale from assigns.

This function is specifically designed for use in component templates where the locale needs to be passed explicitly via assigns.

Prefers base locale code for URL generation (current_locale_base), falls back to extracting base from full dialect code (current_locale).

path(url_path, opts \\ [])

phoenix_kit_app_base()

@spec phoenix_kit_app_base() :: String.t()

Gets the base module name for the parent application.

Reads from :phoenix_kit, :layouts_module config (e.g., MprojectWeb.Layouts -> MprojectWeb).

Examples

iex> PhoenixKit.Utils.Routes.phoenix_kit_app_base()
"MprojectWeb"

url(url_path)

Returns a full url with preconfigured prefix.

This function first checks for a configured site URL in Settings, then automatically detects the correct URL from the running Phoenix application endpoint when possible, falling back to static configuration. This ensures that magic links and other email links work correctly in both development and production environments, with full control over the base URL through the Settings admin panel.

url_prefix()

@spec url_prefix() :: String.t()

Returns the configured PhoenixKit URL prefix.

Examples

iex> PhoenixKit.Utils.Routes.url_prefix()
"/phoenix_kit"