1

i have a php file in life ray for ajax

<?php
//connect to the mysql
$db = @mysql_connect('127.0.0.1', 'root', 'root') or die("Could not connect database");
@mysql_select_db('liferaydb', $db) or die("Could not select database");

//database query
$sql = @mysql_query("select name, status from gb_guestbook");

$rows = array();
while($r = mysql_fetch_assoc($sql)) {
  $rows[] = $r;
}

//echo result as json
echo json_encode($rows);
?>

However as i placed the file under \tomcat-8.0.32\webapps\ROOT and attempted to go to this url: http://localhost:8080/server_processing.php, it says the requested resources could not be found..

However, when i placed a index.jsp inside the same directory after starting the tomcat server, i am able to access it locally. Please advise.

This is my ajax code:

$(document).ready(function() {
    $("#ajaxButton").click(function() {
        $.ajax({
              type: "Post",
              url: "\\server_processing.php",
              success: function(data) {
                    var obj = $.parseJSON(data);      
                    var result = "<ul>"
                    $.each(obj, function() {
                        result = result + "<li>Name : " + this['name'] + " , Status : " + this['status'] + "</li>";
                    });
                    result = result + "</ul>"
                    $("#result").html(result);
Liferayer
  • 17
  • 4

2 Answers2

0

I dont think that your liferay tomcat server can host php files out of the box. But liferay can host php files in portlets. There is a sample php portlet available at sample-php-portlet. Also you can configure your tomcat to serve php files as well: tomcat/UsingPhp or this discussion on stackoverflow run-a-php-app-using-tomcat

Andre Albert
  • 1,386
  • 8
  • 17
  • am i right to say that to host php files in portlet simply means creating a new portlet for my project and then creating a php file under it? – Liferayer Jul 11 '18 at 07:52
  • According to [Liferay PHP](https://web.liferay.com/de/community/wiki/-/wiki/Main/PHP+Portlets) Yes, just name your file index.php. Please also mention your liferay portal version. Maybe this change recently or is dependent on a certain liferay version – Andre Albert Jul 11 '18 at 08:04
  • i am using liferay7.. and my file name is already in php but i am placing it inside the server and trying to access it using localhost. Could this be the problem? i tried placing the file inside my liferay project file and accessing it from there but since its a url field it is redirecting the links to localhost instead of file path (updated in my codes) – Liferayer Jul 11 '18 at 08:52
  • When using a PHP-portlet (i am also not sure if this possible with DXP) you need to create a page and place the portlet first, so that you can have a URL. But i highly recommend @olaf-kock ansqwer. You creating database connection next to your given portal infrastructure environment. May be those article about Liferay7 and JAXRS Services will help you more (eg [liferay 7 JAXRS](http://www.javasavvy.com/liferay-7-rest-webservices-tutorial/). Im pretty shure there is also some sample portlets directly available on github – Andre Albert Jul 11 '18 at 09:21
0

Apart from the ancient php sample portlet (mentioned in Andre Albert's answer) I haven't seen PHP in portlets ever. Your chances of getting good answers are very low. I'm assuming that nobody is using php in the context of writing portlets.

The way you embed PHP files directly in webapps/ROOT however would safely bypass every portal infrastructure, e.g. not even working as a portlet. This would purely just utilize Tomcat as a PHP server, with the requirement to properly enable Tomcat to handle PHP files. IMHO this disables all of the infrastructure that you get from a portal (identity management, interoperability, combine different applications) that you shouldn't go this route.

With regards to PHP portlets on Liferay 7: I haven't even seen the sample portlet. This may be because I didn't pay attention or because it's not there. But, just as I opened this answer, I don't expect it to be in wide use. Or even: I expect it to be not in use.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90