PhoenixMountable.Path (PhoenixMountable v0.2.1)

Copy Markdown View Source

Paths relative to the mount: the ~m sigil.

Inside a mountable library the mount path is the host's choice, so ~p cannot be used. ~m builds a path relative to the mount instead:

use PhoenixMountable.Path, mountable: MyLib.Router

~m"/items/#{item}"          #=> "/admin/my_lib/items/42"
~m"/items?#{[page: 2]}"     #=> "/admin/my_lib/items?page=2"
~m"/"                       #=> "/admin/my_lib"

Like verified routes:

  • paths are checked after compilation against the library's own routes: a path that matches none of them emits a warning naming the file and line;
  • interpolated segments go through Phoenix.Param and are URL-encoded; an interpolated query takes a map or keyword list.

At runtime the mount comes from the variable in scope (assigns, socket or conn, in that order), or from PhoenixMountable.Mount.current/0. The endpoint's script name and path are honoured, like ~p.

Outside of ~m, PhoenixMountable.Path.path/2 builds a path for an explicit mount, socket or conn.

Summary

Functions

Builds the full path of path, relative to the mount of source: a mount, a socket, a conn or an assigns map.

Builds the full path of path under another mount of the same router, by its name (the mount macro, or its :as): a link from one of the library's definitions to another, wherever the host mounted it.

Builds a path relative to the mount. See the module documentation.

Functions

path(source, path)

@spec path(PhoenixMountable.Mount.t() | map(), String.t()) :: String.t()

Builds the full path of path, relative to the mount of source: a mount, a socket, a conn or an assigns map.

path(source, name, path)

@spec path(PhoenixMountable.Mount.t() | map(), atom(), String.t()) :: String.t()

Builds the full path of path under another mount of the same router, by its name (the mount macro, or its :as): a link from one of the library's definitions to another, wherever the host mounted it.

PhoenixMountable.Path.path(conn, :my_lib_page, "/")  #=> "/changelog"

Raises when the host has not mounted it; PhoenixMountable.Mount.sibling/2 returns nil instead, for a link shown only when it has.

sigil_m(arg, modifiers)

(macro)

Builds a path relative to the mount. See the module documentation.