views:

291

answers:

1

The standard way of creating URLs in grails is:

<a href="${createLink(controller:'news', action: 'show', params: [id: news.id])}">${news.title}</a>

which generates the url: /news/show/102

I want more SEO friendly URLs like:

/news/102/this-is-the-hottest-news-today

What is the cleanest way to do this in Grails? I could use grails URLMapping to map /news/show/102 to /news/102, but how I do create the complete URL like above?

+4  A: 

You could turn the headline into a parameter like this:

name story: "/news/$id/$headline" {
    controller = "news"
    action = "show"
}

That way you could create your urls with the headline in them and the mapping would still work. You of course don't actually have to use the headline parameter that will appear in your controller. The example above uses a named URL mapping so you can then say:

${createLink(mapping: "story", params: [id: 102, headline: 'this-is-the-hottest-news-today'])}

You may also be interested in this plugin for creating canonical urls - http://www.grails.org/plugin/canonical

Dave
+1. The 'mapping' argument was what I was needing. Thanks.
Langali