Liferay friendly url


Liferay Friendly URL

In this tutorial, we will see how to make Liferay URL to  friendly. Liferay generates lengthy URL’s for render,action,resource which looks clumsy and not readable and add below URL parameters which are not required.:

  • p_p_id
  • p_p_col_id – page column id
  • p_p_col_count
  • p_p_lifecycle –  0 -> render, 1->action, 2->resource
  • p_p_state – window state
  • p_p_mode – edit, view, help,print, preview,config,edit guest, edit defaults

Lets take below sample Render URL and we will make this URL as friendly as shown below:

 Sample Render URL:  

 http://localhost:8081/web/guest/product?p_p_id=product_WAR_prodcutportlet
  &p_p_lifecycle=0
  &p_p_state=normal
  &p_p_mode=view
 &p_p_col_id=column-1
 &p_p_col_count=1
  &_product_WAR_prodcutportlet_cmd=addProduct

Friendly URL:   Liferay allows you change above URL to below using Friendly URL routes

http://localhost:8081/web/guest/product/-/prod/addProduct

How to configure friendly URL:  

Below are high level steps:

  • Create routes.xml files
  • Update liferay-portlet.xml with Friendly URL configuration  with below attributes:
    • friendly-url-mapper-class
      • Liferay provides com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper class which take care of every thing
      • you can also provide custom friendly URL mapper that implements com.liferay.portal.kernel.portlet.FriendlyURLMapper 
    • friendly-url-mapping
      •   this field used to alternative for portlet name.
      • In this example, we used “prod” as mapping and URL will be: http://localhost:8081/web/guest/product/-/{friendly-url-mapping}/{pattern}.
      • you can give portlet name or some other name that you want to make url more readable.
    • friendly-url-routes:  provide routes XML file path here

Steps for Liferay friendly url mappings:

  • Lets create below project  structure friendly
  • Create package for routes.xml. create routes.xml file in the package : org.javasavvy.product.routes
  • We will create route in routes.xml file for below render request:
  • <portlet:renderURL var="addProdUrl" >
     <portlet:param name="cmd" value="addProduct"/>
    </portlet:renderURL>
  • Expected friendly URL for above  is : http://localhost:8081/web/guest/product/-/prod/addProduct
  • Lets create route for above request in  routes.xml file
    • routes :   It is  root element and holds multiple route
    • route : sub element in routes
    • pattern :   pattern of the mapped friendly URL. The value you provide here will go in {pattern} in below
      • http://localhost:8081/web/guest/product/-/{friendly-url-mapping}/{pattern}.
    • generated-parameter:The generated-parameter element specifies the pattern of a parameter that will be generated from other parameters when a URL is recognized. When a URL is built, these virtual parameters will be parsed from the generated parameter and made available in the route pattern for constructing the URL
    • ignored-parameter : ignored-parameter element specifies a parameter that should be ignored and not included in generated URLs. Ignored parameters do not effect URL recognition.
    • implicit-parameter: The implicit-parameter element specifies a parameter that is not present in the route pattern.
  • <?xml version="1.0"?>
    <!DOCTYPE routes PUBLIC "-//Liferay//DTD Friendly URL Routes 6.2.0//EN" "http://www.liferay.com/dtd/liferay-friendly-url-routes_6_2_0.dtd">
    <routes>
     <route>
          <pattern>/{cmd}</pattern>
          <generated-parameter name="cmd">{cmd}</generated-parameter>
          <ignored-parameter name="p_p_state"/>
          <ignored-parameter name="p_auth"/>
          <ignored-parameter name="p_p_id"/>
          <ignored-parameter name="p_p_col_id"/>
          <ignored-parameter name="p_p_col_count"/>
          <ignored-parameter name="#"/>
        <implicit-parameter name="p_p_lifecycle">0</implicit-parameter>
     </route>
    </routes>
  • Edit liferay-portlet.xml for portlet “product” after icon XML tag:
  • <portlet>
     <portlet-name>product</portlet-name>
     <icon>/icon.png</icon>
     <friendly-url-mapper-class>com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper</friendly-url-mapper-class>
     <friendly-url-mapping>prod</friendly-url-mapping>
     <friendly-url-routes>org/javasavvy/product/routes/routes.xml</friendly-url-routes>
     <header-portlet-css>/css/main.css</header-portlet-css>
     <footer-portlet-javascript>
     /js/main.js
     </footer-portlet-javascript>
     <css-class-wrapper>product-portlet</css-class-wrapper>
     </portlet>

    Liferay sample Friendly URL Routes:

    • Let create friendly URL like below for render URL: editProdUrl variable:
      • Generated friendly URL: http://localhost:8081/web/guest/product/-/prod/editProduct/101 
      • <portlet:renderURL var="editProdUrl" >
         <portlet:param name="cmd" value="editProduct"/>
         <portlet:param name="productId" value="101"/>
        </portlet:renderURL> 
      • <route> 
         <pattern>/{cmd}/{productId}</pattern>
         <generated-parameter name="productId">{productId}</generated-parameter>
         <generated-parameter name="cmd">{cmd}</generated-parameter>
         <ignored-parameter name="p_p_state"/>
         <ignored-parameter name="p_auth"/>
         <ignored-parameter name="p_p_id"/>
         <ignored-parameter name="p_p_col_id"/>
         <ignored-parameter name="p_p_col_count"/>
         <ignored-parameter name="#"/>
         <implicit-parameter name="p_p_lifecycle">0</implicit-parameter>
         </route>

        2.  Generated Friendly url for below request is :http://localhost:8081/web/guest/product/-/prod/addProduct

    • <portlet:renderURL var="addProdUrl" >
       <portlet:param name="cmd" value="addProduct"/>
      </portlet:renderURL>
       <route>
       <pattern>/{cmd}</pattern>
       <generated-parameter name="cmd">{cmd}</generated-parameter>
       <ignored-parameter name="p_p_state"/>
       <ignored-parameter name="p_auth"/>
       <ignored-parameter name="p_p_id"/>
       <ignored-parameter name="p_p_col_id"/>
       <ignored-parameter name="p_p_col_count"/>
       <ignored-parameter name="#"/>
       <implicit-parameter name="p_p_lifecycle">0</implicit-parameter>
       </route> 
    • Hope this helps!!

 

Click here for Liferay 7 friendly URL Tutorial

Liferay 7 Friendly URL tutorial will drive you creation of friendly URL in Liferay 7. Liferay generated URL’s for render,action and resource request are not readable and but liferay allows you make them friendly. Liferay 6…

READ MORE

Leave a Reply