Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get friendly urls to work in Liferay

I wanted to enable friendly urls in liferay for my JSR-268 Portlets.

I configured the friendly url mapper as I was told in http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/FriendlyURLMapper but somehow this does not work for me.

What am I missing?

My liferay-portlet.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.1.0//EN"     "http://www.liferay.com/dtd/liferay-portlet-app_6_1_0.dtd">
<liferay-portlet-app>
<portlet>
    <portlet-name>content</portlet-name>
    <friendly-url-mapper-class>com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper</friendly-url-mapper-class>
    <friendly-url-mapping>content</friendly-url-mapping>
    <friendly-url-routes>com/gentics/cr/portlet/content-url-routes.xml</friendly-url-routes>
    <instanceable>true</instanceable>
</portlet>
<portlet>
    <portlet-name>navigation</portlet-name>
    <icon>/icon.png</icon>
    <instanceable>true</instanceable>
</portlet>
<portlet>
    <portlet-name>search</portlet-name>
    <icon>/icon.png</icon>
    <instanceable>true</instanceable>
    <!-- header-portlet-css>/css/test.css</header-portlet-css>
    <footer-portlet-javascript>/js/test.js</footer-portlet-javascript-->
</portlet>
<!--role-mapper>
    <role-name>administrator</role-name>
    <role-link>Administrator</role-link>
</role-mapper>
<role-mapper>
    <role-name>guest</role-name>
    <role-link>Guest</role-link>
</role-mapper>
<role-mapper>
    <role-name>power-user</role-name>
    <role-link>Power User</role-link>
</role-mapper>
<role-mapper>
    <role-name>user</role-name>
    <role-link>User</role-link>
</role-mapper-->
</liferay-portlet-app>

My content-url-routes.xml looks like this:

<?xml version="1.0"?>
<!DOCTYPE routes PUBLIC "-//Liferay//DTD Friendly URL Routes 6.0.0//EN" "http://www.liferay.com/dtd/liferay-friendly-url-routes_6_0_0.dtd">

<routes>
<route>
    <pattern>/content/{contentid}</pattern>
</route>
<!-- route>
    <pattern>/{instanceId}/search/{filter}</pattern>
    <implicit-parameter name="do">search</implicit-parameter>
</route>
<route>
    <pattern>/{instanceId}/search/{filter}/{start}</pattern>
    <implicit-parameter name="do">search</implicit-parameter>
</route-->
</routes>

The urls rendered in the portlet is still: http://localhost:8080/web/guest/home?p_auth=Yu81QQrj&p_p_id=content_WAR_genticsportlet_INSTANCE_R2HSaoL5RkHi&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-2&p_p_col_count=1&_content_WAR_genticsportlet_INSTANCE_R2HSaoL5RkHi_contentid=10007.33

The URL was created using the follogwing code:

PortletURL actionURL = response.createActionURL();
actionURL.setParameter("contentid", "10007.33");

It would be very nice if you could point me into the right direction...

like image 823
csupnig Avatar asked Apr 04 '12 14:04

csupnig


2 Answers

Your route should start with the part after content as content is already part of the generated URL. The value <friendly-url-mapping>content</friendly-url-mapping> is used for that.

<route>
    <pattern>/{contentid}</pattern>
</route>

Also, be sure to change the DTD to the latest one in your *-url-routes.xml.

<!DOCTYPE routes PUBLIC "-//Liferay//DTD Friendly URL Routes 6.1.0//EN" "http://www.liferay.com/dtd/liferay-friendly-url-routes_6_1_0.dtd">
like image 55
adarshr Avatar answered Nov 15 '22 09:11

adarshr


Following on from Adarshr has answered you can also get rid of the /-/ part by creating your own class that extends DefaultFriendlyURLMapper and overrides the method isCheckMappingWithPrefix so you get:

@Override
public boolean isCheckMappingWithPrefix() {     
    return false;
}

So you're URLs would be just be http://community_name.com/web/group_name/page_name/content/contentId. Then you set your class in the friendly-url-mapper-class tags. Which you can see an example of in this portlet project on GitHub:

https://github.com/DevJonny/Liferay-6-Friendlier-Friendly-URL-Mapper

~~ EDIT ~~

Thanks Prakash for pointing out that the old link didn't work.

like image 21
Jonny Avatar answered Nov 15 '22 09:11

Jonny