gotham icon indicating copy to clipboard operation
gotham copied to clipboard

Bi-directional routing

Open nyarly opened this issue 7 years ago • 2 comments

Especially with the idea of providing for modular web apps, it would be incredibly useful to be able to do something like

Response::new()
  .with_header(Location::new(url_for(other_handler))

(this is intended to echo a Rails feature). In other words: Gotham routing already accomplishes routing from a URL to a handler. What this issue proposes is being able to go from a handler and produce URL(s). (Plural because: relative, same base URL as the request, a canonical base...)

There's a number of ways to accomplish this, but one requirement (which isn't addressed by e.g. strings as url paths) is that routing within a module of handlers still work if they're put within a routing tree (or moved around in the tree.)

A further feature requirement would be the ability to "link up" modules - authentication tools often need to know about where to redirect once a user is authenticated, for instance, and reusability relies on being able to configure this into the modules when they're routed.

nyarly avatar Dec 19 '17 18:12 nyarly

Potentially related to #7. If we could find a way to map handlers back to URLs, that would solve (most of) the "named routes" problem without needing to explicitly name any routes.

smangelsdorf avatar Dec 21 '17 11:12 smangelsdorf

I totally missed #7. From experience implementing this kind of thing, the routes do need some fully-qualified identifier - names are the most obvious solution. If they associate to a handler, and handlers can reference each other (even across modules) that would be a really elegant solution.

What you effectively want to do is say "I want a link that will result in HandlerFoo being called with parameters " (whether that means path interpolation or get params or whatever) - this is one of the reasons I like URI templates for this. It would probably be acceptable to have the routing tree produce a template to a routable node, and then have the template render the uri...

Handlers as names (or even routing nodes as names - since method/content-type/... is outside the URI scope) is a good idea though.

nyarly avatar Dec 21 '17 19:12 nyarly