Giter Site home page Giter Site logo

routex's Introduction

Coveralls Build Status Last Updated Hex.pm Hex.pm

Routex

Routex is a framework to extend the functionality of Phoenix Frameworks' router. Using a pluggable extension system it can transform routes, create alternative routes and generate helper functions based on routes in your Phoenix Framework app. It acts as middleware between route definition and route compilation by Phoenix; in order to have minimal impact on run-time performance.

It ships with a small set of extensions and provides helper functions for writing your own custom Phoenix router features.

Top Features and Benefits

  • none to minimal run-time performance penalty (depending on extensions).
  • adding router features is plug-and-play.
  • include only features you need in your product; less code is less bugs.
  • write powerful extensions without the plumbing.

Example

Localize your Phoenix website with multilingual URLs and custom template assigns; enhancing user engagement and content relevance. This example combines a few extensions: Alternatives, Translations, Assigns and Verified Routes.

                    ⇒ /products/:id/edit                      @loc.locale = "en_US"
/products/:id/edit  ⇒ /eu/nederland/producten/:id/bewerken    @loc.locale = "nl_NL"
                    ⇒ /eu/espana/producto/:id/editar          @loc.locale = "es_ES"
                    ⇒ /gb/products/:id/edit                   @loc.locale = "en_GB"
  • Alternatives: the URL format is customizable (no mandatory website.com/[locale]/page)
  • Translation: URLs match the language of content; enhancing user engagement and content relevance.
  • Assigns: the value of locale is specified per scope by the configuration of the Alternatives extension. The value is made available in components and controllers in namespace loc as @loc.locale
  • Verified Routes allows you to use use ~p"/products/#{product}/edit" in your code. At run-time the route is combined with the set locale to pick the localized alternative route.

Documentation

HexDocs (stable) and GitHub Pages (development).

Requirements and Installation

See the Usage Guide for the requirements and installation instructions.

Extensions

Routex relies on extensions to provide features. Each extension provides a single feature and should minimize hard dependencies on other extensions. Instead, Routex advises to make use of Routex.Attrs to share attributes; allowing extensions to work together whithout being coupled.

The documentation of each extension lists any provided or required Routex.Attrs.

Alternatives

Create alternative routes based on scopes configured in a Routex backend module. Scopes can be nested and each scope can provide it's values to be shared with other extensions.

Alternatives Documentation

Translations

This extension extracts segments of a routes' path to a routes.po file for translation. At compile-time it combines the translated segments to translate routes. As a result, users can enter URLs using localized terms which can enhance user engagement and content relevance.

Translations Documentation

Multilingual Routes

The Alternatives extension can be combined with the Translations extension to create multilingual routes. The Alternatives extension provides the :locale attribute used by the Translations extension.

Verified Routes

This extension creates a sigil (default: ~l) with the ability to branch based on the current alternative scope of a user. It is able to verify routes even when thy have been transformed by Routex extensions. Optionally this sigil can be set to ~p (Phoenix' default) as it is a drop-in replacement.

Verified Routes Documentation

Route Helpers

Creates Phoenix Helpers that have the ability to branch based on the current alternative scope of a user. Optionally these helpers can replace the original Phoenix Route Helpers as they are drop-ins.

Route Helpers Documentation

Assigns

With this extension you can add (a subset of) attributes set by other extensions to Phoenix' assigns making them available in components and controllers with the @ assigns operator (optionally under a namespace)

@namespace.area   =>  :eu_nl

Assigns Documentation

Alternative Getters

Creates a helper function alternatives/1 to get a list of alternative slugs and their routes attributes. As Routex sets the @url assign you can simply get other routes to the current page with alternatives(@url).

Alternative Getters Documentation

Attribute Getters

Creates a helper function attrs/1 to get all Routex.Attrs of a route. As Routex sets the @url assign you can simply get all attributes for the current page with attrs(@url).

This way the assigns can be a subset of the full list of attributes but the full list can be lazy loaded when needed.

Attribute Getters Documentation

Cloak (only for experiments)

Transforms routes to be unrecognizable. This extension is a show case and may change at any given moment to generate other routes without prior notice.

  /products/  ⇒ /c/1
  /products/:id/edit  ⇒ /c/:id/2      ⇒ in browser: /c/1/2, /c/2/2/ etc...
  /products/:id/show/edit  ⇒ /:id/3   ⇒ in browser: /c/1/3, /c/2/3/ etc...

Attribute Getters Documentation

routex's People

Contributors

bartotten avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

sfusato

routex's Issues

chore: deprecate Routex.Extension.RouteHelpers

Phoenix helpers are deprecated so the use of helpers will decline. Also I haven't found a use case in which the helpers of Routex are a better choice than Routex Verified Routes (which are more powerful than the native Phoenix ones)

Strange compilation error: could not load module ExampleWeb.RoutexBackend due to reason :nofile

  1. mix phx.new --no-ecto example
  2. added the dependecy: {:routex, "~> 0.1.0-alpha.2"}
  3. copied over example_web.ex and routex_backend.ex from routex_example app
  4. wrapped the get "/" in router.ex in preprocess_using ExampleWeb.RoutexBackend do block
preprocess_using ExampleWeb.RoutexBackend do
  get "/", PageController, :home
end

I get this compilation error:

Completed: ExampleWeb.RoutexBackend ⇒ Routex.Extension.Alternatives.configure/2
Completed: ExampleWeb.RoutexBackend ⇒ Routex.Extension.Translations.configure/2
Start: Processing routes with Routex.Processing
Completed: ExampleWeb.RoutexBackend ⇒ Routex.Extension.VerifiedRoutes.configure/2

14:08:44.878 [info] 
The default sigil used by Phoenix Verified Routes is overridden by Routex due to the configuration in `ExampleWeb.RoutexBackend`.

      ~p: localizes and verifies routes. (override)
      ~o: only verifies routes. (original)

Documentation: https://hexdocs.pm/routex/extensions/verified_routes.html


== Compilation error in file lib/example_web/router.ex ==
** (ArgumentError) could not load module ExampleWeb.RoutexBackend due to reason :nofile
    (elixir 1.14.0) lib/code.ex:1562: Code.ensure_loaded!/1
    (routex 0.1.0-alpha.2) lib/routex/processing.ex:136: Routex.Processing.transform_routes/3
    (routex 0.1.0-alpha.2) lib/routex/processing.ex:76: anonymous fn/3 in Routex.Processing.execute_callbacks/1
    (stdlib 4.0.1) maps.erl:411: :maps.fold_1/3
    (routex 0.1.0-alpha.2) lib/routex/processing.ex:75: Routex.Processing.execute_callbacks/1
    (stdlib 4.0.1) lists.erl:1350: :lists.foldl/3

I've checked the instructions from here to be sure I've not missed anything.

Couldn't reproduce the error in the routex_example app.

routes.po not being updated/extracted

Used the routex_example for this test.

I've created the gettext folders for en, be locales (nl folder was already created) like this:

mix gettext.merge priv/gettext --locale en
mix gettext.merge priv/gettext --locale be

Next, neither mix gettext.merge priv/gettext nor mix gettext.extract --merge extracted anything.

There's neither a default/initial routes.pot file generated in priv/gettext.

What am I missing? Is there some gettext incantation I'm not doing in the right order?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.