Xenia.Routes (Xenia v0.1.1)

Copy Markdown View Source

Generates Phoenix LiveView routes from a Xenia app folder.

# lib/my_app_web/router.ex
import Xenia.Routes

scope "/" do
  pipe_through :browser

  xenia_routes "lib/my_app_web/app"
end

For a folder like

lib/my_app_web/app/
  page.ex                      defmodule MyAppWeb.HomePage
  projects/page.ex             defmodule MyAppWeb.ProjectsPage
  projects/[id]/page.ex        defmodule MyAppWeb.ProjectDetailsPage
  settings/live.ex             defmodule MyAppWeb.SettingsLive
  settings/profile/page.ex     defmodule MyAppWeb.ProfilePage
  settings/security/page.ex    defmodule MyAppWeb.SecurityPage

this generates ordinary routes, in the order Phoenix should match them:

live "/", MyAppWeb.HomePage
live "/projects", MyAppWeb.ProjectsPage
live "/projects/:id", MyAppWeb.ProjectDetailsPage
live "/settings/profile", MyAppWeb.SettingsLive, MyAppWeb.ProfilePage
live "/settings/security", MyAppWeb.SettingsLive, MyAppWeb.SecurityPage

A standalone page is its own LiveView. Pages under a live.ex share its LiveView, and the page module is the live action, so the owner can render @live_action. Switching between them with <.link patch> keeps the process and its state.

mix phx.routes, ~p verified routes, pipelines and live_session work as usual. Module names are read from the files with Xenia.Routes.PageModule; nothing in them is run at compile time.

Folder and scope

The folder is relative to the project root (where mix runs) and must be a literal string. Module names are used as written: a surrounding scope "/", MyAppWeb alias is not prepended to them.

Authentication

Route groups such as (auth) grant nothing. Put xenia_routes inside the pipe_through and live_session that should protect it.

Recompilation

Editing a page.ex, layout.ex, persistent.ex or live.ex recompiles the router (@external_resource). Adding, moving or removing one changes the folder's file list, which the router's __mix_recompile__?/0 notices on the next mix compile, also through Phoenix's code reloader.

Summary

Functions

The pages in root with their modules and owners, in route order.

Generates a live route for every page in dir. See the module docs.

Functions

routes(root)

@spec routes(Path.t()) :: [Xenia.Routes.Route.t()]

The pages in root with their modules and owners, in route order.

Raises ArgumentError when two files define the same module (almost always a copied file that wasn't renamed).

xenia_routes(dir)

(macro)

Generates a live route for every page in dir. See the module docs.