View Source Routex.Extension.Alternatives (Phoenix Routes Extension Framework v0.1.0-alpha.4)

Creates alternative routes based on scopes configured in a Routex backend module. Scopes can be nested and each scope can provide Routex.Attrs to be shared with other extensions.

configuration

Configuration

# file /lib/example_web/routex_backend.ex
# This example uses a `Struct` for custom attributes, so there is no attribute inheritance;
# only struct defaults. When using maps, nested scopes will inherit attributes from their parent.

+ defmodule ExampleWeb.RoutexBackend.AltAttrs do
+  @moduledoc false
+  defstruct [:contact, locale: "en"]
+ end

defmodule ExampleWeb.RoutexBackend do
+ alias ExampleWeb.RoutexBackend.AltAttrs

use Routex,
extensions: [
+ Routex.Extension.Alternatives
],
+ scopes: %{
+    "/" => %{
+      attrs: %AltAttrs{contact: "root@example.com"},
+      scopes: %{
+        "/europe" => %{
+          attrs: %AltAttrs{contact: "europe@example.com"},
+          scopes: %{
+            "/nl" => %{attrs: %AltAttrs{locale: "nl", contact: "verkoop@example.nl"}},
+            "/be" => %{attrs: %AltAttrs{locale: "nl", contact: "handel@example.be"}}
+          }
+        },
+      "/gb" => %{attrs: %AltAttrs{contact: "sales@example.com"}
+    }
+  }

pseudo-result

Pseudo result

                     /products/:id/edit              locale: "en", contact: "rootexample.com"
/products/:id/edit   /europe/nl/products/:id/edit    locale: "nl", contact: "verkoop@example.nl"
                     /europe/be/products/:id/edit    locale: "nl", contact: "handel@example.be"
                     /gb/products/:id/edit           locale: "en", contact: "sales@example.com"

routex-attrs

Routex.Attrs

Requires

  • none

Sets

  • any key/value in :attrs
  • scope_helper
  • scope_alias
  • scope_prefix
  • scope_opts
  • alternatives (list of Phoenix.Route.Route)