Paul Draper
Paul Draper

Reputation: 83273

Ignoring parameters in routes

One of the nice things about Play is that it doesn't dictate your URL format. That's great because I'm porting an application and need to retain backwards compatibility with old URLs.

I want to match all URLs that start with /foo/bar:

/foo/bar
/foo/bar/0
/foo/bar/1
/foo/bar/baz

How do I do this?

I can't find much documentation. I found old docs for 1.2.7 that said

You can tell Play that you want to match both URLs by adding a question mark after the trailing slash. For example:

GET /clients/? Clients.index

The URI pattern cannot have any optional part except for that trailing slash.

That's a little funny to be its own special case. IDK how much is still true, since it isn't in the current documentation.


I tried

GET     /foo/bar             Foo.bar
GET     /foo/bar/*unused     Foo.bar

and

GET     /foo/bar             Foo.bar
GET     /foo/bar/$unused<.*> Foo.bar

but compilation failed.

Compilation error[Missing parameter in call definition: unused]


Finally, I tried redefining Foo.bar to take a junk argument (with default as the empty string).

GET     /foo/bar             Foo.bar
GET     /foo/bar/*unused     Foo.bar(unused)

and

GET     /foo/bar             Foo.bar
GET     /foo/bar/$unused<.*> Foo.bar(unused)

but it still didn't work.

conflicting symbols both originated in file '/home/paul/my-play-project/target/scala-2.10/src_managed/main/routes_reverseRouting.scala'


How do I match URL prefixes, or ignore parameters?

Upvotes: 3

Views: 1102

Answers (1)

Paul Draper
Paul Draper

Reputation: 83273

The solution is thanks to @wingedsubmariner

GET     /foo/bar             Foo.bar
GET     /foo/bar/$unused<.*> Foo.barIgnoreUnused(unused)

with

object Foo {

  def bar = Ok("")

  def barIgnoreUnused(unused: String) = bar

}

Surprisingly, this doesn't seem to work.

GET     /foo/bar$unused<.*> Foo.bar(unused)

Probably the correct thing for Play to do is allow unused parameters, but this (admittedly ugly) solution works.

Upvotes: 2

Related Questions