an easy way to set optional last forward-slash in routes file

884 views
Skip to first unread message

sas

unread,
May 27, 2012, 7:36:25 PM5/27/12
to play-framework
I'd like the same action to match

/topics
and
/topics/

Is there a simple way to set the last "/" as optional

so far now I'm using this:

GET /topics/ controllers.Topics.list
GET /topics controllers.Topics.list
PUT /topics/ controllers.Topics.update
PUT /topics controllers.Topics.update
POST /topics/ controllers.Topics.save
POST /topics controllers.Topics.save

In play 1.x you used /topics/?

is there something like that in play2?

biesior

unread,
May 28, 2012, 1:29:10 AM5/28/12
to play-fr...@googlegroups.com
ellou' sas

There's no support for trailing slash 'yet' - https://play.lighthouseapp.com/projects/82401/tickets/98-support-for-optional-trailing-slash

Unfortunately although it's uncomfortable situation both routes - with and without trailing slash - also shouldn't send the same resource as search engines doesn't like this. Instead  trailed route should be a redirection to untriled.

GET     /topics            controllers.Topics.list
GET     /topics/           controllers.Topics.redirectToList

BTW. I realized while creating 'dummy' trailed routes for my app that's there's no need to create additional routes for many requests as for an example your POST and PUT requests are called from the page's code so you don't need to care that they will be modified. (In other words you need to care only for the routes for requests that can be modified by user in address bar)

greetings biesior

sas

unread,
May 28, 2012, 2:20:47 AM5/28/12
to play-framework
thanks a lot for the answer

but the app is indeed a json rest web service, that it's called from a
backbone app

that's why I wanted to handle both cases...

I'll give a try to the redirect, but I don't know how well will jquery
handle it...

anyway, if this issue is going to be resolved in the near future, I
wouldn't mind having duplicated entries in my routes file for a couple
of months...

On 28 mayo, 02:29, biesior <bies...@gmail.com> wrote:
> ellou' sas
>
> There's no support for trailing slash 'yet' -https://play.lighthouseapp.com/projects/82401/tickets/98-support-for-...

biesior

unread,
May 28, 2012, 2:28:45 AM5/28/12
to play-fr...@googlegroups.com
Hmm that shouldn't be a problem then, as you are creating the app, where user has no possibility to modify the url of the request, you can and even should normalize that to use only one syntax (trailed or untrailed), it is additionally first step of avoiding request manipulation, however not for long :)

sas

unread,
May 28, 2012, 1:57:11 PM5/28/12
to play-framework
yes, you are right, I will normalize it

any common practice? I rather get rid of trailing slashes, they just
add noise...

saludos

sas

Steven Luu

unread,
May 28, 2012, 4:20:01 PM5/28/12
to play-fr...@googlegroups.com
Here's an example for Scala using Application Global: http://blog.sluu.org/handle-trailing-slash-in-url/

Nico

unread,
May 28, 2012, 4:37:44 PM5/28/12
to play-fr...@googlegroups.com

biesior

unread,
Jun 30, 2012, 4:51:08 PM6/30/12
to play-fr...@googlegroups.com
Bored with manual adding slash and no slash routes for each GET i just wrote drastic simple catch all route with the slash - it does the trick for all GETs :)

As a last route in the routes file:

# catch all route with slash at the end, pointing to action containing movedPermanently()
# redirect without the trailing slash
GET     /*path/             controllers.Application.redirectUntrailed(path: String)

in the Application.java:

    /**
     * Moves permanently the get into version without trailing slash
     * @param path String
     * @return Result
     */
    public static Result redirectUntrailed(String path) {
        //    route required somwhere at the end:
        //    GET    /*path/    controllers.Application.redirectUntrailed(path: String)
        return movedPermanently("/" + path);
    }

As you can see, there's more comments then code :)

Serega Sheypak

unread,
Jun 21, 2016, 3:43:04 AM6/21/16
to play-framework
Hi, It doesn't work for me some reason... redirectUtrailed is not invoked... Ho can I track it?

суббота, 30 июня 2012 г., 22:51:08 UTC+2 пользователь biesior написал:
Reply all
Reply to author
Forward
0 new messages