[2.0-scala] custom route with :

141 views
Skip to first unread message

Razvan Cojocaru

unread,
May 4, 2012, 11:55:27 AM5/4/12
to play-fr...@googlegroups.com
I got used to the routes file to some extend - I'm trying to use a route like wikipedia's categories: /wiki/CATEGORY:PAGE mapped to show(CATEGORY,PAGE)

but I can't quite do it. here's some of the versions I tried, each has an issue

GET     /wiki/$cat<[^:/]*>$c<:>$name<.+>   controllers.Wiki.show2(cat, name, c) //ugly with the extra c
GET     /wiki/$cat<[^:/]*:>$name<.+>   controllers.Wiki.show1(cat, name) // can't reverse it nice

is there a way to specify the <:> static pattern in there?

thanks.

biesior

unread,
May 4, 2012, 1:16:08 PM5/4/12
to play-fr...@googlegroups.com
You can pass it to router as a single param let's call it 'target':

GET     /wiki/:target   controllers.Application.wiki(target)

In Java I would write it as this, you just need to write the same in Scala:

    public static Result wiki(String target) {
        String[] splited = target.split(":");
        return (splited.length == 2 && splited[0].equals("Category"))
                ? ok("Display category: " + splited[1])
                : ok("Search for article: " + splited[0]);

Razvan Cojocaru

unread,
May 4, 2012, 1:19:01 PM5/4/12
to play-fr...@googlegroups.com
thanks - how do i reverse that?

routes.Wiki.show(category,name) would be agnostic to the separator used there...?

biesior

unread,
May 4, 2012, 1:41:26 PM5/4/12
to play-fr...@googlegroups.com
As router expects single string, you need to join it to single string in your view (I assumed that 'Category' is a keyword like in original Wiki):

@for(category <- categories){
    <a href='@routes.Application.wiki("Category:"+category.pathName)'>@category.name</a>
}

@for(article <- articles){
    <a href='@routes.Application.wiki(article.pathName)'>@article.name</a>
}

Of course as  you can see you need to add fields ie pathName to your models to allow finding it by String not by Long id, like in real Wikipedia:

domain.tld/wiki/Category:Play_Framework_2.0    (as link to category to Play Framework 2.0)
domain.tld/wiki/Using_custom_divider    (as link to article)

Razvan Cojocaru

unread,
May 4, 2012, 2:55:32 PM5/4/12
to play-fr...@googlegroups.com

I’m sorry – I guess my question is: is there a way to escape the : character in a route?

 

The point is that this works just as expected (using HABIBI as separator) – works both ways:

 

GET     /wiki/$cat<[^:/]*>HABIBI$name<.+>   controllers.Wiki.show2(cat, name, c=":")

 

So you basically recognize ANY string in there. However, this doesn’t work (using : as separator):

 

GET     /wiki/$cat<[^:/]*>:$name<.+>   controllers.Wiki.show2(cat, name, c=":")

 

Because the : has a special meaning for routes.

 

Thanks,

Razie

--
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/-/PRin3ItFeocJ.
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.

biesior

unread,
May 4, 2012, 3:21:57 PM5/4/12
to play-fr...@googlegroups.com
I don't know the way for escaping this char, so I suggested you a workaround :) that's all what I can do

Razvan Cojocaru

unread,
May 7, 2012, 6:14:17 PM5/7/12
to play-fr...@googlegroups.com

Appreciate that – wondering though if there is a solution or we need a defect – I think this deserves a defect since the idea is in fact meant to work.

 

I see the parser for the routes uses this rule for the respective dynamic and static parts

 

  def singleComponentPathPart: Parser[DynamicPart] = (":" ~> identifier) ^^ {
        case name => DynamicPart(name, """[^/]+""")
      }

 

 

 def staticPathPart: Parser[StaticPart] = (not(":") ~> not("*") ~> not("$") ~> """[^\s]""".r +) ^^ {

        case chars => StaticPart(chars.mkString)

      }

 

While the path starts with the “bad” one so I couldn’t find a workaround

 

def path: Parser[PathPattern] = "/" ~ ((positioned(singleComponentPathPart) | positioned(multipleComponentsPathPart) | positioned(regexComponentPathPart) | staticPathPart) *) ^^ {
        case _ ~ parts => PathPattern(parts)

--

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/-/4_LofDghECQJ.

Nilanjan

unread,
May 7, 2012, 6:56:09 PM5/7/12
to play-framework
Does this work for you?

/wiki/$cat<[^:/]*>$c<[:]{1}>$name<.+> controllers.Wiki.show2(cat,
name, c)

Nilanjan

On May 7, 4:14 pm, "Razvan Cojocaru" <ra...@razie.com> wrote:
> Appreciate that - wondering though if there is a solution or we need a
> defect - I think this deserves a defect since the idea is in fact meant to
> work.
>
> I see the parser for the routes uses this rule for the respective dynamic
> and static parts
>
>   def singleComponentPathPart: Parser[DynamicPart] = (":" ~> identifier) ^^
> {
>         case name => DynamicPart(name, """[^/]+""")
>       }
>
>  def staticPathPart: Parser[StaticPart] = (not(":") ~> not("*") ~> not("$")
> ~> """[^\s]""".r +) ^^ {
>
>         case chars => StaticPart(chars.mkString)
>
>       }
>
> While the path starts with the "bad" one so I couldn't find a workaround
>
> def path: Parser[PathPattern] = "/" ~ ((positioned(singleComponentPathPart)
> | positioned(multipleComponentsPathPart) |
> positioned(regexComponentPathPart) | staticPathPart) *) ^^ {
>         case _ ~ parts => PathPattern(parts)
>       }
>
> From: play-fr...@googlegroups.com
> [mailto:play-fr...@googlegroups.com] On Behalf Of biesior
> Sent: May-04-12 3:22 PM
> To: play-fr...@googlegroups.com
> Subject: Re: [play-framework] Re: [2.0-scala] custom route with :
>
> I don't know the way for escaping this char, so I suggested you a workaround
> :) that's all what I can do
>
> W dniu piątek, 4 maja 2012 20:55:32 UTC+2 użytkownik Razvan Cojocaru
> napisał:
>
> I'm sorry - I guess my question is: is there a way to escape the : character
> in a route?
>
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visithttps://groups.google.com/d/msg/play-framework/-/4_LofDghECQJ.

Razvan Cojocaru

unread,
May 7, 2012, 6:59:01 PM5/7/12
to play-fr...@googlegroups.com
Yes, that's similar to what I ended up using - the reverse however is kind
of ugly, since I have to encode the : everywhere, i.e.

href="routes.Wiki.show2(cat,name,":")"

thanks
Reply all
Reply to author
Forward
0 new messages