Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework - Redirect with params

I am trying to figure out how to do a redirect within a controller action in Play (2.0) using Scala.

The redirect using

Redirect(routes.Application.index)

works just fine.

What I cannot figure out from the docs, API, or Google is how to add parameters to the call.

I am coming from Grails where this could be done easily as follows:

redirect action: "index", params: ["key": "value"] .

The only way I have found is to call Redirect using a string url and a query string, which seems awkward.

Basically I would like to make use of Redirect(Call) somehow, but I do not how to create the Call object using reverse routing.

Am I missing something/not getting the concept in Play/Scala?

Thanks in Advance!

like image 252
pchronz Avatar asked Apr 04 '12 21:04

pchronz


2 Answers

Ellou'

A route is just a function, so you can pass arguments as usual:

// Redirect to /hello/Bob
def helloBob = Action {
    Redirect(routes.Application.hello("Bob"))    
}

This snippet comes from http://www.playframework.org/documentation/2.0/ScalaRouting (at the bottom)

like image 98
biesior Avatar answered Sep 25 '22 23:09

biesior


You can also avoid creating another function just for this in your controller. In your route config, you can simply add something like this:

  GET  /google  @controllers.Default.redirect(to = "http://google.com")
like image 34
Yawo Guillaume Kpotufe Avatar answered Sep 25 '22 23:09

Yawo Guillaume Kpotufe