elixir-plug / plug

Compose web applications with functions
https://hex.pm/packages/plug
Other
2.84k stars 582 forks source link

More flexible dynamic routes with `:` parameters #1183

Closed mikatuo closed 10 months ago

mikatuo commented 10 months ago

Current behavior

Valid routes:

get "/js/script-:app_id/"
get "/images/image-:id"

Invalid routes:

get "/js/script-:app_id.js"
get "/js/script-:app_id-v1.js"
get "/images/:id.png"

^ they will raise "(Plug.Router.InvalidSpecError) invalid dynamic path. Only letters, numbers, and underscore are allowed after :"

A workaround would be to use a *name parameter and then parse it in a controller.

Expected behavior

In addition to / allow usage of - or . after a :param in dynamic routes.

whatyouhide commented 10 months ago

I don't have a strong preference, but I’m afraid that this will grow to make pretty complex routes, so maybe the suggestion to use *params in the route and then parse it in a controller is the most future-proof one.

josevalim commented 10 months ago

We compile our routes to pattern matching and the VM does not support suffix matches at the moment. So this feature needs to be added upstream first. For now the best option is to match on everything and then you dispatch accordingly.