trie-router icon indicating copy to clipboard operation
trie-router copied to clipboard

What about generating url for the route?

Open th0r opened this issue 11 years ago • 8 comments

Is it possible to generate url for specific route not to hardcode them in templates?

th0r avatar Jan 26 '14 19:01 th0r

not sure what you mean. example?

jonathanong avatar Jan 26 '14 20:01 jonathanong

Something like this:

app.get('post', '/post/:id', function *() {...});

// Generates url for post with id = 1
var url = app.pathForRoute('post', { id: 1 });
// ==> '/post/1'

th0r avatar Jan 26 '14 21:01 th0r

oh i c. i see a lot of edge cases with that. not totally against it though, but i would never use it.

jonathanong avatar Jan 26 '14 21:01 jonathanong

Why not? Almost every serious web framework has such feature. It's very useful and helps you follow DRY principle. I was very surprised that even Express doesn't have this feature...

th0r avatar Jan 27 '14 06:01 th0r

i see a lot of edge cases with that

What edge cases do you mean?

th0r avatar Jan 27 '14 06:01 th0r

yeah i've never seen the need for it. easier just to do '/post/' + id.

just stuff like routes without names, not having all the names defined, etc. i could making the definitions stricter and throw if not all the names are provided in the helper or something.

jonathanong avatar Jan 27 '14 07:01 jonathanong

Needs for it are simple:

  1. You don't need to think about url parts encoding.
  2. If route url changes you will not need to change it in all the templates it is used in.
  3. Ability to connect your sub-application to some prefixed path (e.g. "app-admin" to "/admin") without changing any urls in "app-admin" templates.

th0r avatar Jan 27 '14 08:01 th0r

+1

pauliusuza avatar Feb 02 '15 16:02 pauliusuza