It’s been a while since I written a post, but last Thursday I was working on Liferay portlet which involved using Liferay’s built-in Friendly URL Mapping functionality, and I hit a small snag which I thought I’d write about now.
I’m going to assume that you already know about Friendly URL Mapper in Liferay 6. If not then you should take a look at the Liferay Wiki page on it, or get the great Liferay In Action book, then come back here!
The Friendly URL Mapper is great. It’s quick and simple to set up, and hides a lot of the complexities of creating friendly URLs in Liferay. However it has one characteristic that I had to get rid of for my portlet. This was the Friendly URL Mapper prefix! If you have a look at the URL below between the two mentions of /wiki/ you’ll see what I’m talking about:
http://www.liferay.com/community/wiki/-/wiki/Main/FriendlyURLMapper
That /-/ is the Friendly URL Mapper prefix, and to my mind looks really bad! However I believe the reason behind it’s existence is also shown in the above URL. The prefix signifies the beginning of the Friendly URL, and the end of the actual URL. So you can have a page named the same as the as name of the mapping (as you can see the page above is “wiki” and the mapping is also “wiki”. With the code I’m showing you below, this won’t work so bare that in mind!
So there are two changes you need to make to your existing Friendly URL Mapping code to remove that prefix. The first is to create a new a class along the lines of MyPortletNameFriendlyURLMapper. This class needs to extend com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper. Then you all you need to do is override the isCheckMappingWithPrefix method of the BaseFriendlyURLMapper, by returning false. The code for this class can be seen below:
package com.mysmallcornerstudios.friendlierurlmapping.portlet;
import com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper;
public class FriendlierFriendlyURLMapper extends DefaultFriendlyURLMapper {
public boolean isCheckMappingWithPrefix() {
}
}
Then all we have to do is update the liferay-portlet.xml to say use our new FriendlyURLMapper class instead of the Default one, but the Default one is still doing most of the work for us. So in my liferay-portlet.xml I just change the <friendly-url-mapper-class> to the below:
<friendly-url-mapper-class>
</friendly-url-mapper-class>
That’s it! Job done! So an example of what we’ve done here is change this URL:
http://mysite.com/somepage/-/friendlier/someParameter/
to:
http://mysite.com/somepage/friendlier/someParameter/
So it’s even friendlier to look at! The source code for this can be found in my GitHub repository called Liferay-6-Friendlier-Friendly-URL-Mapper. I’ve also added a WAR file to my repository so you can easily deploy this portlet. The WAR file can be found here.
I hope this helps people!