Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate complex url like stackoverflow?

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.

like image 352
Freewind Avatar asked Jan 15 '11 08:01

Freewind


2 Answers

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)

like image 112
Ricardo Avatar answered Nov 18 '22 12:11

Ricardo


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

like image 2
Genius Avatar answered Nov 18 '22 12:11

Genius