The play framework has a function in the view to create an SEO friendly URL via the slugify()
function. There does not seem to be an "unslugify" function, so how do you lookup models from the slugified parameter?
If for instance I have a Blog model which has a title property set to "hello world", slugify would produce "hello-world". If I then perform a query Blog.find("byTitle", title)
where title is the slugified title, it will return no results. How can you lookup a model using the provided slug?
There doesn't appear to be a method, but I am not surprised. Slugify removes characters from the string and unslugify would not know where to put it back in.
For example, if you look at the URL for this question, it is
stackoverflow.com/questions/4433620/play-framework-how-do-i-lookup-an-item-from-a-slugify-url
It has removed the exclamation (!), parentheses and the quotes from the title of this question. How would an unslugify method know how and where to put those characters back in?
The approach you want to take is to also include the ID, just as the stackoverflow URL has.
If you wanted to take the same format as the stackoverflow URL, your route would be
GET /questions/{id}/{title} Question.show()
Then in your action, you would ignore the title, and simply do Blog.findById(id);
You then have a SEO friendly URL, plus use a good REST approach to accessing the Blog post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With