-
Notifications
You must be signed in to change notification settings - Fork 20
Liferay Friendly URL
The normal generated portlet URLs containing a set of internal liferay request parameters.
These long URLs of links or forms are mostly not readable and its not easy to share it somewhere else.
General liferay portlet URL:
http://localhost:8080/web/guest/examples?p_p_id=example_WAR_ExamplePortlet&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_example_WAR_ExamplePortlet_javax.portlet.action=new
Explanation of the liferay request parameters:
p_p_id: The portlet ID (example_WAR_ExamplePortlet)
p_p_state: Liferay windows pages state - 1 (normal) 2 (maximize) 3 (minimize)
p_p_mode: Mode of the portlet loke like - view edit help
p_p_lifecycle: this is life cycle of portlet 0 (render) 1 (action) 2 (server)
p_p_col_id: The reference ID of the column in liferay template
p_p_col_pos: Specifiy the column position if the the layout having more than one columns.
p_p_col_count: ...
Liferay provides a mechanism to shorten the generated urls by using the Friendly URL mapper feature.
How to configure the friendly URL Mapper in Liferay?
Configuration of URL routes in XML files
CREATE example-friendly-url-router.xml
<?xml version="1.0"?>
<!DOCTYPE routes PUBLIC "-//Liferay//DTD Friendly URL Routes 6.2.2//EN"
"http://www.liferay.com/dtd/liferay-friendly-url-routes_6_0_0.dtd">
<routes>
<route>
<pattern>/action/{actionName}</pattern>
<generated-parameter name="javax.portlet.action">{actionName}</generated-parameter>
<ignored-parameter name="p_auth"/>
<ignored-parameter name="p_p_id"/>
<implicit-parameter name="p_p_lifecycle">1</implicit-parameter>
<implicit-parameter name="p_p_state">normal</implicit-parameter>
<implicit-parameter name="p_p_mode">view</implicit-parameter>
</route>
</routes>
Explanation of the liferay friendly mapper route parameters:
routes: routes element which contains all routes entry
route: single route element entry
pattern:
generated-parameter:
ignored-parameter:
implicit-parameter:
overridden-parameter:
It is necessary to order the parameters as described above.
These files should located in the resources folder otherwise tomact can not initialize the XML routes.
Configuration of friendly URL Java class
MODIFY liferay-portlet.xml
<friendly-url-mapper-class>com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper</friendly-url-mapper-class>
<friendly-url-mapping>example</friendly-url-mapping>
<friendly-url-routes>com/.../example-friendly-url-routes.xml</friendly-url-routes>
In the next step we need one java implemetation class to generated the liferay friendly urls.
Liferay provides the "DefaultFriendlyURLMapper" class to create the urls based on our rules.
The liferay friendly url mapper configuration is placed after and before tag.
Liferay will generate the following friendly URL
http://localhost:8080/web/guest/examples/-/example/action/new
* Liferay then will add – (dash) * Next is submit-name which is same as element’s value. It means whatever value we give to this element, will be appended after – (dash) in final friendly URL * Next to it is /add which is nothing but the pattern value in router (friendly-url-router.xml) file.
Friendly URL mapper functionality is not working if the portletURL API is used in local javascript. It is helpfull to generate portlet:renderURL placeholder and replace them by using dummy values.