6

I'm using playframework, and I hope to generate complex urls like stackoverflow. For example, I want to generate a question's url:

http://aaa.com/questions/123456/How-to-generator-a-complex-url

Note the last part, it's the title of the question.

But I don't know how to do it.

UPDATED

In the playframework, we can define routes in conf/routes file, and what I do is:

GET /questions/{<\d+>id} Questions.show

In this way, when we call @{Questions.show(id)} in views, it will generate:

http://aaa.com/questions/123456

But how to let the generated has a title part, is difficult.

Freewind
  • 193,756
  • 157
  • 432
  • 708

2 Answers2

11

With playframework it's easy to generate such url. In your routes file you add this :

GET /questions/{id}/{title}       YourController.yourMethod

See the doc in playframework site about routing for more info

In your html page :

<a href="@{YourController.yourMethod(id,title.slugify())}">

slugify method from JavaExtensions, clean your title from reserved characters (see doc)

Ricardo
  • 858
  • 4
  • 7
2

It a server-side url rewriter does. In case of SO it doesn't matter you type {...}/questions/4698625/how-to-generate-complex-url-like-stackoverflow or {...}/questions/4698625 - they both redirects to the same content. So this postfix is used just to increase readability of a url.

To see more details about url rewriting, see this post.

UPD: to generate such a postfix,

  1. take a title of the content,
  2. shrink multiple whitespaces into single
  3. replace all whitespaces with dash (-)
  4. remove all non-letter symbols from a title

Better to perform this operations with Regular Expressions

Community
  • 1
  • 1
Genius
  • 1,784
  • 14
  • 12