Custom Router in playframework

353 views
Skip to first unread message

laguiz

unread,
Feb 19, 2011, 12:59:42 PM2/19/11
to play-framework
Hi guys,

I need to custom how the Route generate the reverse in Play!

Basically I would like to happen to all my @{} one string before
"mystring/"

For instance, instead of getting "/login" I need "mystring/login". I
need this everywhere :p Ok that's strange but trust me I really need
it ;) And I need to keep my root = to '/login'.

Do you have any advice to custom how @{} reverse the route?

Thanks,
Maxence


laguiz

unread,
Feb 19, 2011, 1:04:11 PM2/19/11
to play-framework
Sorry with less errors :

Hi guys,
I need to custom how the class Route generate the reverse in Play!
Basically I would like to append to all my @{} one string before like
"/mystring"
For instance, instead of getting "/login" I need "/mystring/login".
I
need this everywhere. Ok that's strange but trust me I really need
it ;) And I need to keep my route equals to '/login'.
Do you have any advice to custom how @{} reverse the route?
Thanks,
Maxence



murz

unread,
Feb 19, 2011, 4:13:39 PM2/19/11
to play-framework
So I did a little digging, Router.load() actually takes a prefix. It
looks like it is used by the ServletWrapper to set the contextPath
from Servlet configuration.

I may be mistaken, but I do not see any way to cleanly set the
contextPath if you aren't using servlets.

However, I do see a way you could hack it. Since Play's ctxPath
property is public, you could create a PlayPlugin class in a new
module, and set the value of Play.ctxPath when your plugin gets
loaded.

It would look something like this:

public class ContextPathPlugin extends PlayPlugin {

@Override
public void onLoad(){
Play.ctxPath = "/mystring";

laguiz

unread,
Feb 27, 2011, 11:14:36 AM2/27/11
to play-framework
Thank you murz.

I'm using your trick by using Play.ctxPath and it's works like a
charm.

I do it on application start using @OnApplicationStart.

Thanks,

sas

unread,
Feb 27, 2011, 3:46:47 PM2/27/11
to play-framework
dont forget that route.conf file is treated by play like a template...

that is, you can mix groovy code, so something like (I'm just
improvising)

%{
prefix = "/mystring"
}%

GET ${prefix}/login Applicacion.login()
...


should work...

laguiz

unread,
Feb 28, 2011, 2:23:09 PM2/28/11
to play-framework
Hey I missed that! I will take try. And let you know. Thank you.
Reply all
Reply to author
Forward
0 new messages