Combo.Router.Helpers (combo v0.8.0)

View Source

Generates a module for named routes helpers and generic url helpers.

Named routes helpers exist to avoid hardcoding routes, if you wrote <a href="/login"> and then changed your router, the link would point to a page that no longer exist. By using router helpers, you can make sure it always points to a valid URL in your router.

Generic url helpers exist for convenience.

Examples

defmodule MyApp.Web.Router do
  use Combo.Router
  # ...
end

It will generated a module named MyApp.Web.Router.Helpers, and following functions are available.

Name routes helpers:

  • *_url/_
  • *_path/_

Generic url helpers:

  • url/_
  • path/_
  • static_url/_
  • static_path/_
  • static_integrity/_

Forwarded routes

Forwarded routes are also resolved automatically. For example, imagine you have a forward path to an admin router in your main router:

defmodule MyApp.Web.Router do
  # ...
  forward "/admin", MyApp.Web.AdminRouter
end

defmodule MyApp.Web.AdminRouter do
  # ...
  get "/users", MyApp.Web.Admin.UserController
end

Summary

Functions

Receives a route and returns the quoted definition for its helper function.

Generates the helper module for the given environment and routes.

Callback for generate router catch all.

Functions

def_helper(route, exprs)

Receives a route and returns the quoted definition for its helper function.

In case a helper name was not given, or route is forwarded, returns nil.

def_helper_catch_all(arg)

define(env, routes)

Generates the helper module for the given environment and routes.

defs()

expand_segment(segment)

expand_segments(segments)

raise_route_error(mod, fun, arity, action, routes, params)

Callback for generate router catch all.