Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically get the url of a page in liferay

I want to create a link in a portlet so that I can navigate to a different page in the liferay portal. I order to do that I am looking for an API (can be liferay specific) that given a page name, would return it's url (it can be the friendlyURL as well).

like image 279
bernardn Avatar asked Jan 27 '11 13:01

bernardn


People also ask

How do I get a URL for Liferay?

RE: how to get current URLgetURLPortal()+themeDisplay. getURLCurrent()" which gives me exactly the URL that is currently shown in the browser with parameters and everything..


2 Answers

Portal pages in Liferay are indeed called Layouts in APIs and DB tables. They're identified by plid field, can be obtained using LayoutLocalServiceUtil and related APIs, and also from some other calls like themeDisplay.getLayout().

However in order to build String containing URL to a page you'll have to concatenate friendly URL of portal, group and layout itself (i.e. /web/guest/home - web is portal URL for public pages, guest is friendly URL of guest group by default and home is friendly URL of home page/layout by default). This can be tricky, as you have to check whether this is a public or private page, etc.

And once you start using virtual hosts with friendly URLs for groups, things change. So this is not a good way.

To avoid manually creating URLs and have URLs that are guaranteed to be correct you should use com.liferay.portal.util.PortalUtil.getLayoutFriendlyURL(Layout layout, ThemeDisplay themeDisplay) a static method of PortalUtil - it'll do all the necessary work for you. Though you also need to provide ThemeDisplay and not only Layout.

like image 165
mvmn Avatar answered Sep 22 '22 09:09

mvmn


The API to access pages in Liferay is the LayoutService. However, page names are not unique in Liferay and furthermore they are internationalized. So you need some unique property for a page to retrieve its url, besides its name.

If you really only have the page name, you can use LayoutLocalServiceUtil.getLayouts(...) to loop over all Layouts and check for some property (in this case its name).

like image 31
p.mesotten Avatar answered Sep 22 '22 09:09

p.mesotten