Liferay is a Gartner Magic Quadrant Leader for the Sixth Year! Find out why
« Back to Portlets

PHP Portlets

Alternative Languages: 日本語

Introduction #

This page outlines Liferay's PHP portlet feature.

One of the main differences between a normal PHP application and a Liferay PHP application is the way URL parameters are handled. For example, look at the following urls:

PHP: http://www.mysite.com/page.php?id=1

JAVA: http://www.mysite.com/web/guest/page?p_p_id=helloworldphpapp_WAR_helloworldphpapp&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&p_p_col_id=column-3&p_p_col_count=1&_helloworldphpapp_WAR_helloworldphpapp_phpURI=index.php?id=1

The first URL is probably what you are used to seeing: the URL parameter of id is being passed to the PHP page called page.php. The second URL is a Liferay specific URL used to access a specific app on a Liferay page.

Liferay URL Parameters #

p_p_id: this parameter is used to tell which portlet to access.

p_p_lifecycle: this parameter is used in telling Liferay which action to perform. This is a binary value (0 or 1). 0 simply tells Liferay to just render the portlet, whereas 1 tells Liferay to call the process Method of a StrutsAction.

p_p_state: this parameter is a unique parameter, in that, it is used in helping with Liferay pop ups and AJAX calls. In the current URL above, the parameter is set to normal, therefore, everything is rendered as it should. This parameter also has other values, such as exclusive, maximized and minimized.

  • exclusive state tells the page to ONLY render that particular portlet
  • maximized state is used in telling liferay to render the portlet (when placed on a page) in full, works kinda like if you were to have a collapsible div in javascript.The display below shows you what adding a new Web Content Display looks like. Surrounding this display item there is a border with a few buttons to click on to perform actions for this particular portlet. This is to demonstrate the Maximized and Minimized States for a portlet. Clicking on the Minus sign will collapse the div, whereas, clicking on the Plus sign will Maximize the div.
  • minimized state (shown above) is used to display the portlet collapsed.

p_p_mode: this parameter acts as a Struts Action parameter. Just like a switch statement in PHP, this parameter is used to tell which Struts Method to render. This is mainly seen/used while in the control panel when you are editing a created page or editing your personal profile.

p_p_col_id and p_p_col_count: these parameters are used in aiding the Liferay templating system.

_helloworldphpapp_WAR_helloworldphpapp_phpURI: This parameter is used to send your PHP page(s) URL parameters and to tell which page to render inside the portlet. In the URL above we have it set to index.php?id=1. To render a different page inside (and I will explain about this a little later when going over developing a portlet with more than one PHP page) this portlet simply change the index.php to the name of the page you want to render (anotherpage.php?id=1).

Dissecting the final parameter

Take note that the parameter contains the name of the PHP app (helloworldapp). To access your app this parameter would simply change to the name of your app (mypersonalapp).

The Development Process #

Ok, now that the URL parameters have been explained, let’s dig into the actual development process.

Coding #

Since Liferay is scripting language agnostic, you can create a PHP app just like you would create any other PHP app. The syntax does not need to change to meet Liferay standards. Although there are some differences in how the code is handled, most of your practices will stay in tact.

There are some “tricks” and “hacks” I will explain later that will aid in finishing your app.

Hello World Portlet #

  1. Open up your favorite Text Editor to start a new project. I will be using TextMate for this demonstration.
  2. Begin your app by creating a simple ‘Hello World’ echo statement.
  3. So now that we have our code written, we need to package the files for installation.
  4. Open up a terminal window
  5. Navigate to where you have saved your PHP app
  6. Next you are going to run a zip command to package your application
  7. Run the following shell command://sudo zip helloworldapp.zip index.php
  8. Now that we have our zipped file, it is time to install this file in Liferay. Log into your liferay environment and navigate to the control panel
  9. Click on Plugins Installation, under the Server tab
  10. Click on the Install More Portlets buttons
  11. Click on the Upload File tab
  12. Click on the browse button and navigate to where you have saved your zip file
  13. In the textbox below the upload field type in the name of the application. If you do not type in a name, the name of your application will be the name of your zip file. I recommend using a name without spaces.
  14. Once you have typed in the name you would like to call your application, click Install
  15. Congratulations! You have now successfully installed your first PHP Liferay Application!

Testing your application #

Now that you have installed your application, it is time to test it out.

  1. While still in the control panel, click on the communities link under the portal tab
  2. Next click on the Actions button next to the community you would like to use, then click Manage Pages in the actions panel. For this instance we will be using the Guest community
  3. Once in the Manage Pages section, you will be greeted with a list of all of the pages you have created along with a few other options to better manage your pages. We will be focusing on the section that is already pulled up on the screen. In the textbox on the screen type in the name of the page you would like to create. Remember, this is the page we will be deploying our application to, so you want to name it accordingly.
  4. Do not worry about the Type dropdown, but make sure to click on the Hidden checkbox, this hides the page from showing up in the navigation bar. Once you are satisfied with your page name, click Add Page.
  5. Congratulations! You have just created the page where your PHP app will reside. Now, lets navigate to that page from selecting it from the list on the left so we can check the URL.
  6. Make sure to click on the Page tab once you have clicked on the created page.
  7. Look for the Friendly URL option in the list of items. The URL in this case is: http://liferay.dennismonsewicz.com/web/guest/hello-world-app
  8. Ok, now that we know what our URL is going to be, navigate to that page in your browser. When you first navigate there, you will see a blank page
  9. Using the dock in the upper right hand corner, navigate to the Add Application link
  10. To find your hello world application, simply begin typing the name of the application in the textbox. It will then show up under the Undefined section in the list.
  11. To add the application to the page, simply click the Add button next to the name of your app

Advanced PHP Portlet Development #

Advanced applications can consist of a PHP App that contains libraries of code, MySQL integration, multi-page apps, etc.

MySQL Integration #

When dealing with a PHP CMS, a MySQL ORM is usually built into the backend and is readily available for use in development. Since Liferay is a Java based CMS, “tapping” into its ORM is somewhat a mystery in PHP.

SQL calls are made in the same way as any regular PHP app.

Please note that the file name is index.php. For Liferay to render your application on the webpage, the main file must be called index.php

Multi-Page Applications #

$_POSTing #

As you can see from above, you simply create a form and then post to your second page (I have called my 2nd page results.php, but you can call it whatever you like).

So, what does this look like in Liferay?

Remember, you have to zip the files you are using in order to install the application in Liferay.

Multipage apps with AJAX #

In this section I want to go over something that will come in very handy, utilizing AJAX in your applications.

Coding Magic #

The screenshots from above show the results from an AJAX call in Liferay.

Styles

<style type="text/css"> span { display: block; font-weight: bold; }</style>

Keep in mind, that you can include your own external stylesheets. Including styles in an external CSS file would look something like this:

<link href=”/name_of_application/name_of_folder/name_of_stylessheet.css” rel=”stylesheet” />

Side note – When you install an application in Liferay, it is added to the webapps folder under your tomcat instance. To view or include files from this application, follow the path convention from above.

JavaScript

<script type="text/javascript">

jQuery(document).ready(function(){

var url = "/web/guest/post-test-app?p_p_id=ajaxtest_WAR_ajaxtest&p_p_lifecycle=0&p_p_state=exclusive&p_p_mode=view&_ajaxtest_WAR_ajaxtest_phpURI=results.php";

jQuery('#ajax').click(function(){

jQuery.ajax({

url: url,

success: function(html) {

jQuery('#content').append(html); } });

return false; }); });

</script>}}}

A lot of CMS’ come packaged with certain JavaScript frameworks. Liferay comes packaged with jQuery. Being able to use the jQuery framework makes javascript development a lot easier and faster.

HTML

<p>Hello World!</p>

<p>

<a href="#" id="ajax">Click Me!</a>

</p>

<div id="content"></div>}}}

Results.php

<?php

echo 'Results Rock!';

?>}}}

Note: The code shown above is only one, simple way to use AJAX in your code.

PHP includes in Liferay #

Including a page in your PHP app is just as simple as you remember

<?php

include “pagename.php”;

?>}}}

31 Attachments
91269 Views
Average (4 Votes)
The average rating is 5.0 stars out of 5.
Comments
Threaded Replies Author Date
This looks like a great doc! Would you give... Ray Augé March 2, 2010 11:08 AM
I would be honored to for this to be converted... dennis monsewicz March 3, 2010 11:58 AM
Wow, this looks amazing! Thanks for converting... dennis monsewicz May 6, 2010 9:43 AM
No problem! I wrote an OpenOffice .odt ->... Ray Augé May 26, 2010 11:06 AM
Great doc. How can i get UserId from PHP ? ... ALLYNDREE Pierre November 25, 2010 12:24 AM
Still confused about the exact url setup (and... John Hayward December 1, 2010 1:19 PM
Ok - I finally got things to work - here is... John Hayward December 2, 2010 12:57 PM
The HelloWorldApp works fine in 5.2.3 Can i put... Jörg Halitschke December 17, 2010 4:25 AM
With Luminis 5 which is built on Liferay 5... John Hayward February 5, 2011 7:11 PM
There's one major problem with this: you can't... Jörg Halitschke March 10, 2011 12:19 AM
This is a great article for people like me to... Prakash Khanchandani June 24, 2011 1:29 AM
How could i implement friendly URL in PHP... Manikandan S October 8, 2012 1:34 AM
I created a php web forms portlet utilizing the... John Nguyen November 2, 2012 7:29 AM
I have downloaded Liferay 6.1 (CE). I am unable... Rohit Gaikwad January 31, 2013 8:59 PM
Hey, Rohit. You have to download the private... John Nguyen February 1, 2013 7:05 AM
WOW that is a messy url from liferay. John Nguyen February 1, 2013 7:06 AM
I want to specify several parameters for php... Sergei Vatshekin April 15, 2013 12:30 PM
Can i specify "friendly url" for different php... Sergei Vatshekin April 15, 2013 12:48 PM
Hi Guys, How to implement portlet... Raju oukanti September 10, 2014 3:27 AM
If anybody is struggling with $_POST in 6.2... James Stuart Milne January 16, 2015 11:43 AM

This looks like a great doc! Would you give permission to convert it wholesale to HTML as the basis of this Wiki page?

This way people (like myself) can contribute some insight on how to do some other particularly unclear things!
Posted on 3/2/10 11:08 AM.
I would be honored to for this to be converted to HTML! You 110% have my permission emoticon
Posted on 3/3/10 11:58 AM in reply to Ray Augé.
Wow, this looks amazing! Thanks for converting this over to HTML!
Posted on 5/6/10 9:43 AM.
No problem! I wrote an OpenOffice .odt -> Creole Wiki exporter (based on SUN's Media Wiki exporter, not from scratch).
Posted on 5/26/10 11:06 AM in reply to dennis monsewicz.
Great doc.

How can i get UserId from PHP ?

Are all the functions available ?

Thanks.
Posted on 11/25/10 12:24 AM.
Still confused about the exact url setup (and no success yet)
There seem to be 3 things which need to be set:
1) The last part of the url path (intro this is page -- Ajax this is post-test-app)
2) The name surrounding WAR (intro this is helloworldapp -- Ajax this is ajaxtest)
3) The name following URI (intro this is index.php -- Ajax this is Result.php)

For Ajax it seems #3 should be the php page which computes and produces the text which Ajax is going to use.

My confusion is what should #1 and #2 be set to?

Having the exact directory structure and file names would help.

I am running this on Liferay 6.0 and am getting an exception on the server when I put in the name of the application in #1 and #2.

Suggestions?
Posted on 12/1/10 1:19 PM.
Ok - I finally got things to work - here is what helped me.

#1 the last part of the url path should be the url one sees in the web browser - I guess it has to do with where the portlet is installed.

#2 is the name of the portlet - this is the name which is specified in the optional context when uploading the zip file as a war file. I think one should always set this optional context since the name picked when installing a zip file is filename.zip.

johnh...
Posted on 12/2/10 12:57 PM in reply to John Hayward.
The HelloWorldApp works fine in 5.2.3
Can i put also a complex PHP-App (e.g. REDAXO CMS) into the Portal? That would be simply amazing!
Posted on 12/17/10 4:25 AM in reply to John Hayward.
With Luminis 5 which is built on Liferay 5 accessing mysql is quite a bit more involved. It may well be that using mysql with php under Liferay 6 is this easy.

With Liferay 5 (at least the version which comes with Luminis 5) the mysql_connect fails to connect. There has been a thread - google for liferay php mysql and look for the php+mysql thread.

I was successful by using PDO, adding a resource in META-INF/context.xml, adding a resource-ref in WEB-INF/web.xml.

One poster indicates it is possible to use the mysql_connect function if you "pass the database through to resin" which can be done in web.xml as well. I have not tried this (yet).

At any rate with at least version before Liferay 6 there seems to be a lot of hassle getting a resource set up so that you can connect to the database (probably more better resource allocation if multiple users and multiple portlets are connecting to the same database with the same credentials).

If you are having trouble getting mysql to work with php look at the other thread for specifics on how to get it setup.
johnh...
Posted on 2/5/11 7:11 PM.
There's one major problem with this: you can't set any external href or form action inside a php-portlet, see:
http://www.liferay.com/community/forums/-/message_boards/message/7833987#_19­_message_2155769

so it's mostly useless in this case emoticon
Posted on 3/10/11 12:19 AM.
This is a great article for people like me to get started.
Thanks a lot. emoticon
Posted on 6/24/11 1:29 AM in reply to Jörg Halitschke.
How could i implement friendly URL in PHP portlet?
This link for jsp portlet(http://www.liferay.com/community/wiki/-/wiki/Main/FriendlyURLMapper).
Wha­t about php portlet?
Posted on 10/8/12 1:34 AM in reply to Prakash Khanchandani.
I created a php web forms portlet utilizing the php mail function... I continuous am getting an error that states, "Could not connect SMTP host: localhost, port 25 ."

What is interesting is that when I use a liferay web forms portlet that is not in php its works fine. What am I doing wrong?
Posted on 11/2/12 7:29 AM in reply to Mani kandan.
I have downloaded Liferay 6.1 (CE).
I am unable to find the upload file tab under Plugin Installer section for uploading the zip file(PHP App).

Can someone tell me how to find it?
Posted on 1/31/13 8:59 PM.
Hey, Rohit.
You have to download the private plugin from the marketplace. Here's the link to the CE version, http://www.liferay.com/marketplace/-/mp/application/15474932?_7_WAR_osbportlet_b­ackURL=%2Fmarketplace%3Fp_p_id%3D7_WAR_osbportlet%26p_p_lifecycle%3D0%26p_p_stat­e%3Dnormal%26p_p_mode%3Dview%26p_p_col_id%3Dcolumn-2%26p_p_col_count%3D1%26_7_WA­R_osbportlet_jspPage%3D%252Fmarketplace%252Fsearch.jsp%26p_r_p_564233524_assetCa­tegoryId%3D11232561%26p_r_p_564233524_keywords%3D%26_7_WAR_osbportlet_advancedSe­arch%3Dfalse%26_7_WAR_osbportlet_andOperator%3Dtrue%26_7_WAR_osbportlet_resetCur­%3Dfalse%26_7_WAR_osbportlet_delta%3D100
Posted on 2/1/13 7:05 AM in reply to Rohit Gaikwad.
WOW that is a messy url from liferay.
Posted on 2/1/13 7:06 AM in reply to John Nguyen.
I want to specify several parameters for php file, e.g. ...helloworldphpapp_WAR_helloworldphpapp_phpURI=index.php?id=1&test=1. And I can get only "id" in php file ($_GET["id"]). Is there any way to get "test"? $_GET["test"] is undefined.
Posted on 4/15/13 12:30 PM.
Can i specify "friendly url" for different php page, e.g. http://host/page/-/phpportlet/index2.php? Method described here (http://www.liferay.com/documentation/liferay-portal/6.0/development/-/ai/option­al%3A-adding-friendly-url-mapping-to-the-portlet) doesn't work with php portlet.
Posted on 4/15/13 12:48 PM in reply to Sergei Vatshekin.
Hi Guys,

How to implement portlet configuration page in the php portlet ,controller class and as well as service builder.

Thanks,
Raju
Posted on 9/10/14 3:27 AM.
If anybody is struggling with $_POST in 6.2 version, try putting the portlet prefix in the value field for the parameter _SamplePHP_WAR_samplephpportlet_foo would be the name of the foo Post parameter
Posted on 1/16/15 11:43 AM.