Scala & Play: regexing route without identifier

I want to provide an optional set of my routes in the Play app. For example:

/path/1 /paths/1 

The url of the route I tried was something like this:

 /path<[s?]>/:id 

If I put only $ in front of it, it asks for an identifier; if I gave him an identifier, he tells me that I need to use it in the call definition. Is what I'm trying to do without having to do the Cartesian product of every possible combination / of plurality?

+7
scala regex
source share
2 answers

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

So it seems that the only option is to add a new parameter:

 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 return routes.

+3
source share

You will need to follow the compiler rule to make 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 may need to make sure that this path does not overlap with other routes.

+2
source share

All Articles