Search code examples
javaliferay-6

Get Layout Friendly URL In Liferay


I want to get a friendly URL for a layout based on layout id. For example, /web/group/page. Currently this is how I do it:

Layout layout = LayoutLocalServiceUtil.getLayout(groupId, false, layoutId);

String groupFriendlyUrl = GroupLocalServiceUtil.getGroup(groupId).getFriendlyURL(); //will output /group
String layoutFriendlyUrl = layout.getFriendlyURL(); //will output /page
String webFriendlyUrl = String.format("/web%s%s", groupFriendlyUrl, layoutFriendlyUrl); //will output /web/group/page

I am wondering if there is a better way to do this where I can the full path, /web/group/page with one method.


Solution

  • If you have plid (Page layout Id), use getLayoutFriendlyURL() like below:

    Layout selectedLayout = LayoutLocalServiceUtil.getLayout(plid);
    String url = PortalUtil.getLayoutFriendlyURL(selectedLayout, themeDisplay);