PhoenixKit.Utils.Routes (phoenix_kit v1.7.33)

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 the path to the AI endpoints page.

Returns the default admin locale (base code).

Returns a locale-aware path using locale from assigns.

Returns a full url with preconfigured prefix.

Returns the configured PhoenixKit URL prefix.

Functions

ai_path()

Returns the path to the AI endpoints page.

get_default_admin_locale()

Returns the default admin locale (base code).

This is the first language in the admin_languages setting list, extracted to its base code (e.g., "en-US" becomes "en").

Falls back to "en" if no admin languages are configured.

Examples

iex> Routes.get_default_admin_locale()
"en"  # or "ko" if Korean is first in admin_languages

Use Case

This is used automatically in the on_mount hook to set current_locale in socket assigns. LiveViews can then simply use:

locale = params["locale"] || socket.assigns[:current_locale]

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 \\ [])

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"