This is a simple Grails plugin which helps to easily convert any string into a SEO-friendly one,
eg, from "The Lord of the Rings"
to "the-lord-of-the-rings"
.
Useful if you want SEO-friendly URL's like /book/the-lord-of-the-rings
instead of /book/show/123
.
The code is borrowed from Wordpress's formatting.php, and initially ported to Groovy by Jesús Lanchas.
The plugin provides a simple Grails service, friendlyUrlService
, which you can inject like any other service in your application.
That service has only one mehod, sanitizeWithDashes(text)
.
For convenience, the method asFriendlyUrl()
is also added to the String meta class.
So, given this domain class:
class Book {
String title
String sanitizedTitle
def beforeValidate() {
if (!sanitizedTitle) sanitizedTitle = title?.asFriendlyUrl()
}
static constraints = {
sanitizedTitle unique:true //As an alternative, you may decide to make sanitizedTitle replace the default id.
}
}
And given the following URL mapping:
class UrlMappings {
static mappings = {
"/book/$sanitizedTitle"(controller:'book', action:'show')
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
}
}
You can do the following in your controller:
class BookController {
def show() {
[book: Book.findBySanitizedTitle(params.sanitizedTitle)]
}
}
Note that you can also use friendlyUrlService.sanitizeWithDashes()
in your controller.
The following is a snippet of the provided Spock unit test:
string | sanitized
"The Lord of the Rings" | "the-lord-of-the-rings" //Basics
"Raúl González Blanco" | "raul-gonzalez-blanco" //Accents
"España" | "espana" //N-tilde chars
"Los 3 Mosqueteros" | "los-3-mosqueteros" //Numbers
"Real Madrid® C.F." | "real-madrid-cf" //Edge cases