Caravela.Router (Caravela v0.13.2)

Copy Markdown View Source

Router macros that register a Caravela domain's frontend routes at compile time. The router stays in sync with the domain's DSL — add an entity, regenerate, restart the server, and the routes exist.

Replaces the paste-snippet workflow shipped through v0.11: instead of mix caravela.gen.live printing live "/books", … lines for the developer to paste into router.ex, the developer writes one line and the macro expands into the full route list based on each entity's frontend / realtime? declaration.

Usage

defmodule MyAppWeb.Router do
  use Phoenix.Router
  use Caravela.Router
  import CaravelaSvelte.Router  # needed for :rest entities

  scope "/", MyAppWeb.Library do
    pipe_through :browser
    caravela_routes MyApp.Domains.Library
  end
end

Inside the scope, caravela_routes/1 expands into:

  • live "/<plural>", <Entity>Live.Index, :index (and :new, :show, :edit) for every frontend: :live entity.
  • caravela_rest "/<plural>", <Entity>Controller (plus realtime: true when the entity opts in) for every frontend: :rest entity.

Module names stay unqualified in the expansion so Phoenix's scope alias: resolution kicks in — drop the caravela_routes call inside a scope Foo.BookWeb.Library do … end and BookLive.Index becomes Foo.BookWeb.Library.BookLive.Index, matching what Caravela.Gen.LiveView emits.

Options

caravela_routes/2 accepts the same options as Phoenix's live_session/2 — pass them through when grouping multiple entities behind a common on_mount hook:

caravela_routes MyApp.Domains.Library,
  session: [on_mount: {MyAppWeb.Auth, :require_user}]

:session is a keyword list passed verbatim to Phoenix's live_session/3. When present, the expansion wraps the :live routes in a live_session block so every generated LiveView participates in the shared session.

Versioned domains

A domain declared with version "v1" names its modules MyApp.Library.V1.Book etc. The macro respects that — it emits V1.BookLive.Index so Phoenix's scope alias keeps resolution working across both plain and versioned layouts.

Summary

Functions

Register every route for a Caravela domain.

Functions

caravela_routes(domain_ast, opts \\ [])

(macro)

Register every route for a Caravela domain.

Expanded at compile time by loading the domain module (via Code.ensure_compiled!/1) and reading its entity list. See moduledoc for full semantics.