Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala & Play: route regex without identifier

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?

like image 326
Richard Pianka Avatar asked Dec 26 '13 19:12

Richard Pianka


People also ask

What is Scala used for?

Scala is used in Data processing, distributed computing, and web development. It powers the data engineering infrastructure of many companies.

Is Scala better than Java?

The Advantages of ScalaScala has an exact syntax, eliminating boilerplate code. Programs written in Scala require less code than similar programs written in Java. It is both an object-oriented language and a functional language. This combination makes Scala the right choice for web development.

Is Scala similar to Python?

If we look “under the hood,” Scala is an object-oriented programming language that also incorporates a lot of the functional programming attributes a language like Python is loved for. However, Scala is a distinctly different language than Python.

Is Scala similar to C++?

Scala's is geared more towards compile-time safety with generic code working properly at runtime, while C++ is geared more towards code generation via templates. The C++ type system is way safer than that of Scala. The latter type system has no real distinction between compile-time and runtime.


1 Answers

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.

like image 173
Nicolas Cortot Avatar answered Sep 18 '22 14:09

Nicolas Cortot