Play route syntax for ignoring a part of slug

2019-06-20 08:17发布

What we want is basically this:

/foo/*                 controllers.FooController.foo

However this doesn't work.

We have found the following workaround:

/foo/*ignore           controllers.FooController.foo(ignore)

But this makes the code of the method controllers.FooController.foo slightly ugly. Is there a better way to do this?

1条回答
Bombasti
2楼-- · 2019-06-20 08:42

Looking at the code over here, the router isn't able to deal with the "slug" part without specifying an identifier... the parser combinator doesn't declare it as optional, and the map (^^) is clearly using it as is.

It could be a good feature request if it wouldn't induce other problems where a pattern will hide all other routes because it's defined higher in the file (or even worst, included).

And it looks like it has been done on purpose if we look here, we can figure out that dynamic parameter cannot be assigned a default value -- indeed, in this case we'll fall in the case I've just mentioned :-/.

My first advice would be to tell you to use ignore as an Option[String] and the action definition to set it as None (rather than an empty String because it's more expressive). My second would be to incite you to wonder if such case is really relevant, because it's error prone and could hide further problems

查看更多
登录 后发表回答