Nex.RouteDiscovery (nex_core v0.4.3)

Copy Markdown

Discovers and matches dynamic routes from file system structure.

Supports:

  • [param] - Single dynamic parameter (e.g., users/[id].ex matches /users/123)
  • [...param] - Catch-all parameter (e.g., docs/[...path].ex matches /docs/a/b/c)
  • Mixed routes (e.g., files/[category]/[...path].ex)
  • Nested dynamic routes (e.g., users/[id]/profile.ex)

Summary

Functions

Clears the route cache. Called when files change during development.

Discovers all routes from the given source directory. Returns a list of route definitions.

Gets or initializes the route cache for an application. Routes are cached in ETS for performance.

Matches a URL path against discovered routes. Returns {:ok, module_name, params} or :error

Unified route resolution entry point.

Functions

clear_cache()

Clears the route cache. Called when files change during development.

discover_routes(src_path, type \\ :pages)

Discovers all routes from the given source directory. Returns a list of route definitions.

get_routes(src_path, type)

Gets or initializes the route cache for an application. Routes are cached in ETS for performance.

match_route(routes, url_path, app_module, prefix)

Matches a URL path against discovered routes. Returns {:ok, module_name, params} or :error

resolve(type, path)

Unified route resolution entry point.

Examples

resolve(:pages, ["users", "123"])
# => {:ok, MyApp.Pages.Users.Id, %{"id" => "123"}}

resolve(:api, ["users"])
# => {:ok, MyApp.Api.Users, %{}}

resolve(:action, ["todos", "create_todo"])
# => {:ok, MyApp.Pages.Todos, "create_todo", %{}}

resolve(atom, path, referer_path \\ [])