« Back to PHP Portlets

Working with Complex PHP Portlets (and Ajax)

Includes #

PHP includes work with any files relative to the uploaded files in the ZIP. For example your index file may look like:

<?php
	include 'database.class.php';
	$db = new DBClass();

	include 'config/config.php';
?>

The file structure of your portlet will look like:

index.php
database.class.php
config/
	config.php

POST and GET #

Both the $_POST and $_GET arrays are available to a PHP portlet through their normal syntax.

POST variables should be unaffected. Use them as normal.

GET variables can be passed through HTML by using normal relative URL paths. For example:

<?php /* some PHP code here */ ?>
<a href="index.php?a=my_action&id=<?=$item_id;?>">click here</a>

Redirects #

Normal PHP header redirects do not work.

Ajax (XHR) #

Performing any XMLHTTPRequest requires you know the entire portlet URL, but changing key variables to receive the desired return type (JSON, XML, HTML, etc.). For example:

jQuery.ajax({
	url: "http://domain.com/web/path/pagename"+
		"?p_p_id=portletname_WAR_portletname&p_p_lifecycle=0&p_p_state=exclusive"+
		"&p_p_mode=view&_portletname_WAR_portletname_phpURI=results.php?s=" + s,
	success: function(html) {
		jQuery("#resultLinks").show();
		jQuery("#searchResults").append(html);
	}

Javascript #

Beware that Liferay strips out new lines on the rendered code. Therefore, only block comments will work in Javascript. For example:

Bad:

// this comment will break anything trailing it
var item = someItem;

In the above example "var item=someitem;" will never be evaluated.

Good:

/* this comment will not break anything */
var item = someItem;
0 Attachments
4412 Views
Average (0 Votes)
Comments

Showing 2 Comments

Sverker Abrahamsson
9/25/09 3:50 AM

The php portlet isn't really usable for anything but very simple php scripts. The PHPPortlet use Quercus for executing the php and that part works fine, it's the post processing which isn't enough. I've looked into it and for example it doesn't handle paths correctly, doesn't rewrite other tags than a and form etc. I'm working on addressing those issues but it's not so easy to accomplish as the whole page returned by php needs to be converted.

If you want to deploy a complex php application under Liferay then there are a few alternatives:

* Use Quercus to deploy the php app as a separate war on the same app server. Quercus is a java implementation of php, 5.2 I think.

* Use native php with Tomcat, there are several descriptions on the net on how to get php5 to run under tomcat.

* Use native php with Apache.

For all three variants you then iframe the app into the portal. Integration between the portal and the app, such as single sign on etc, is a separate issue. It's probably easiest to solve with Quercus.

I'm testing the first variant now in parallel to looking more deeply into the PHPPorlet.

Jakub B
3/31/10 8:22 AM

@Sverker Abrahamsson, have you gotten anywhere with your search into the PHPPortlet?

Looking at this as an alternative to JAVA development for Liferay.