New Router features in Plug 1.3

I blogged about a new feature that I developed for Plug that was merged in and released in the brand-new version 1.3.0.

If you’re familiar with Phoenix, you might know that its Router macros all take a plug module as an argument (a Controller is just a Plug). In Phoenix that looks like:

get "/hello", HelloPlug, options

Now you can do a similar thing in Plug Router:

get "/hello", to: HelloPlug, init_opts: options

In previous versions, if you wanted to route (not forward) to another plug, you’d have to do it manually:

get "/hello" do
  HelloPlug.call(conn, HelloPlug.init(options))
end

This new feature opens up possibilities for dispatching requests to Controller-like resource modules, rather than filling up the router with function bodies.

In addition, any values for dynamic routes like get "/hello/:name" will be saved in conn.params and conn.path_params.

Looking forward to you using this!

11 Likes

Nice, thanks, good job!

1 Like