Current route / call in Play Framework 2.0

768 views
Skip to first unread message

EECOLOR

unread,
Mar 1, 2012, 5:10:57 PM3/1/12
to play-framework
Hello,

I can not find a way to get the current route / call from within an
action. How can I get from a request to a route / call?

This would be helpful for libraries that need to perform some work
(login or something) and then redirect back. It would also be handy
for navigation and setting the correct element in the navigation to an
active state.


Thanks,


Erik

EECOLOR

unread,
Mar 4, 2012, 6:11:32 AM3/4/12
to play-framework
It turns out this can be done easily:

Call(request.method, request.path + "?" + request.rawQueryString)

Erik



--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.


atomamo

unread,
Mar 4, 2012, 7:49:27 AM3/4/12
to play-fr...@googlegroups.com
Could you elaborate a little more? is that a Scala call? I couldn't find a Java equivalent to that.

Thanks.


On Sunday, March 4, 2012 6:11:32 AM UTC-5, EECOLOR wrote:
It turns out this can be done easily:

Call(request.method, request.path + "?" + request.rawQueryString)

Erik


On Thu, Mar 1, 2012 at 11:10 PM, EECOLOR <eec...@gmail.com> wrote:
Hello,

I can not find a way to get the current route / call from within an
action. How can I get from a request to a route / call?

This would be helpful for libraries that need to perform some work
(login or something) and then redirect back. It would also be handy
for navigation and setting the correct element in the navigation to an
active state.


Thanks,


Erik

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

EECOLOR

unread,
Mar 4, 2012, 5:39:43 PM3/4/12
to play-fr...@googlegroups.com
Yes, in Scala there is a type of class called a 'case class'. This creates a 'companion' object with the same name and an apply method. Companion object means: an object with the same name that can be accessed in a static fashion. The apply method is executed if you call a method without a name on an object.

The call class is defined like this: case class Call(method: String, url: String) { }

The equivalent of this without the case keyword would be a class and an object:

class Call(method: String, url: String) { }

object Call {
   def apply(method: String, url:String):Call = new Call(method, url)
}

This means that Call(..., ...) has the same result as new Call(..., ...), which is an instance of Call.

My guess is that the Java equivalent would look something like this: 

new Call(request.method(), request.path() + "?" + request.rawQueryString())


Erik

mzafer

unread,
Mar 19, 2012, 5:43:40 PM3/19/12
to play-fr...@googlegroups.com
Hi Erik,

The call usage you have given above is to create a new Call with a known url, but do you know how to get the full url given a call or route. I tried to achieve this using  reverse routing using the the code "controllers.routes.Secure.Login().url" but it returns only the "/login" and not the full url. Any thoughts on how to get the full url.  I am trying to pass the full url to a thirdy party library.

Thanks
Zafer 

EECOLOR

unread,
Mar 20, 2012, 7:25:38 PM3/20/12
to play-fr...@googlegroups.com
The call class has a method called 'absoluteURL' which is defined like this:

def absoluteURL(secure: Boolean = false)(implicit request: RequestHeader) = {
    "http" + (if (secure) "s" else "") + "://" + request.host + this.url
}

You could probably use that.

If any committer of the play framework happens to read this. I would suggest that the implementation is changed to something like this:

def absoluteURL(secure: Option[Boolean] = None)(implicit request: RequestHeader) = {
     secure.map( if(_) "s" else "" ).map("http" + _ + ":").getOrElse("") + "// + request.host + this.url
}

This way it defaults to http or https depending on the current url.


Erik

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/Ne3025cVdvAJ.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages