Scala & Play: route regex without identifier
Asked Answered
G

2

7

I'd like to provide optional plurality to my routes in a Play application. For example:

/path/1
/paths/1

The route URL I tried was something like this:

/path<[s?]>/:id

If I put just a $ in front of it, it asks for an identifier; if I given it an identifier, it tells me I need to use it in the call definition. Is what I'm trying to do possible without having to do a Cartesian product of every possibly with/out combination of plurality?

Guardhouse answered 26/12, 2013 at 19:0 Comment(0)
S
5

If my understanding of the RoutesCompiler is correct (especially the check function), any dynamic part in the URL must be used in the call definition.

So it looks like the only option is adding a new parameter like this:

GET    /$p<paths?>/:id         controllers.PathController.get(p, id)
GET    /otherpath$p<s?>/:id    controllers.OtherController.get(p, id)

Then just ignore the p parameter. You will need to provide it when using reverse routes though.

Succinctorium answered 26/12, 2013 at 22:45 Comment(0)
S
4

You will have to follow the compiler rule to compose the route.

Playframework Documentation

Dynamic parts with custom regular expressions You can also define your own regular expression for the dynamic part, using the $id syntax:

GET   /clients/$id<[0-9]+>  controllers.Clients.show(id: Long)  

In your case, try the following:

GET     /results/$departure</?.+>           @controllers.SearchController.results(departure: String)

However, you might need to make sure this path doesn't overlap with the other routes.

Sicanian answered 15/1, 2016 at 4:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.