jon hanson
jon hanson

Reputation: 9410

How to reverse generate a URL from a route on Play 2?

Under Play 1.4.x if I wanted to reverse generate a URL I might use something like:

Map<String, Object> map = new TreeMap();
map.put("myParam", myParam);
ActionDefinition ad = Router.reverse("MyAction.query", map);
String url = ad.url;

According to the unfailingly terse Play 2.x documentation, under Play 2 with Scala I'm supposed to use a Redirect:

val action = Action {Redirect(routes.MyAction.query(myParam))}

but what do I do with the action to acquire the actual URL?

Upvotes: 2

Views: 4240

Answers (1)

Somatik
Somatik

Reputation: 4743

val relative = routes.MyAction.query(myParam).url

or absolute

val url = routes.MyAction.query(myParam).absoluteURL()

or absolute https

val securedUrl = routes.MyAction.query(myParam).absoluteURL(true)

Upvotes: 11

Related Questions