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;