Reverse Dispatching

Overview

Dispatching allows requests sent to user-friendly URLs to be routed to the appropriate controller functions. It provides a one-way only mapping, which means that the user-friendly URLs must be constructed in all your templates consistently with the dispatch.conf file. The new reverse dispatching feature (available since the 1.3 release) of e_dispatcher alleviates this by creating the user-friendly URL for you based on your dispatch.conf file.

dispatch.conf

Reverse dispatching requires different entries than the original dispatching feature. It is however fully backward compatible with existing dispatch.conf files, so old and new style entries can coexist in your configuration file.

In a reverse-dispatching compatible entry, each variable within the URL is marked by a colon, while the route's name and the patterns for the variables are defined in a forth option list:

{dynamic, "/blog/:user_id/post/:post_no/read", {blog, read}, [{route_name, user_post_read},
                                                              {patterns, [
                                                                {user_id, "[a-zA-Z_0-9]+"},
                                                                {post_no, "[0-9]+"}
                                                              ]}
                                                             ]
}.

Dispatching works the same way for these routes as described in the dispatcher section: a request coming for the URL "/blog/zed/post/108/read" will be dispatched by e_dispatcher to the blog:read/1 function with the property list [{user_id, "zed"}, {post_no, "108"}].

URL generation

You can request e_dispatcher to generate a URL for you by calling the e_dispatcher:url_for/2 function, with the first argument being the route name and the second being a property list for the given route. If the property list is correct and the route exists, the function returns the URL as a string. Note that the values in the property list can be atoms or an integers as well as a strings.

e_dispatcher:url_for(user_post_read, [{user_id, zed}, {post_no, 1}]).
-> "/blog/zed/post/108/read"

In case of an error, the url_for function returns {error, Error}, where Error is:

ReverseDispatching (last edited 2009-05-30 12:45:54 by ZoltanLajosKis)