View Full Version : Laszlo and PHP
scheidecker
10-16-2004, 12:35 AM
Hello,
I have an entire website writen with php. The website uses MySQL for everything, even to control sessions for the users who log in. I have been reading about Laszlo and I found it quite interesting. My PHP apps gerenate HTML screens that are the interface to the user. Stuff like a login screen, browser windows, input forms, etc. I use a sessionid to go from you page to another in order to keep state for user information and other database related information that is pertinent to the system.
My question here is, instead of making php output html to browsers, is it possible to output lazslo docs? That is, how do I integrate Lazslo to PHP so that I can have all the programming from PHP with all the apearance and behaviour of a Lazslo app? What I want to to learn how to integrate one to another. I've noticed that this website here is also php driven.
Thanks in advance. Sorry for the typos, it is late and I am tired.
Welcome to another trying to integrate PHP and Laszlo ..
If you search the forum for "PHP" you will find some threads.
I posted some experiments here ..
wishlist forum:-
http://www.laszlosystems.com/developers/community/forums/showthread.php?threadid=758
tools, development and practices forum:-
http://www.laszlosystems.com/developers/community/forums/showthread.php?threadid=1021
...
You will need to run two concurrent servers
Apache + PHP on say port 88
Tomcat + Laszlo on say port 8080
and connect the two servers using mod_jk2 connector (if you have Apache2).
You can use IIS instead of Apache.
screen scraping .. generating lzx code in php .. using php curl function
http://www.laszlosystems.com/developers/community/forums/showthread.php?threadid=1093&highlight=php
Apache httpd.conf
Depending on your php configuration you will need to add new extensions .. .plzx
if you wish to serve SVG content you will need to add ..psvg
# ---------------------------------------------------------------------------------
# below is PHP 4 setting
# LoadModule php4_module c:/php/sapi/php4apache2.dll
# AddType application/x-httpd-php .php .phtml .psvg .plzx
# ---------------------------------------------------------------------------------
# below is PHP 5 setting
LoadModule php5_module c:/php/php5apache2.dll
AddType application/x-httpd-php .php .phtml .psvg .plzx
# ---------------------------------------------------------------------------------
You will get the idea by reading this article ..
http://www.carto.net/papers/svg/samples/serverside_svg_php.shtml
Early days ..
Late correction to posting ..
in above extension mapping .. instead of the proposed pseudo extension .plzx .. simply add .lzx
i.e.
LoadModule php5_module c:/php/php5apache2.dll
AddType application/x-httpd-php .php .phtml .psvg .lzx
scheidecker
10-18-2004, 03:32 PM
Hello,
Thanks for the reply for my questions.
I still have some questions concerning lzx generation through php or any other scripting or cgi language.
First, the URL http://jakarta.apache.org/tomcat/connectors-doc/jk2/jk2/confighowto.html
Explains how to configure mod_jk2. I've already have it configured.
Is there any special thing that should be done for lzx?
You have showed your modifications to http.conf where you add a new extention to be understood by php, that is .plzx. OK, but what this will do is to associated .plzx with php as if the extetion was .php. Why did you do that? That is, what was your reason for that? How does the functionality differs that just using .php?
In the manual, there is an explanation that if you have an extention such as .php.lzx the lazslo engine would strip .lzx from the extention and then run the app as php. Well,I've tried that but it did not work.
Also, on the PHP script I guess I would need to use the header function.
in this case, what shall I do?
header("Content-type: xml");
or do I create another content type within /etc/mime.types ?
For instance, for xml I have:
application/xhtml+xml xhtml xht
You also mentioned the cURL function from PHP. How could I use it to generate a lzx file that would be interpreted by Laszlo? Could you give me a sample code snippet?
Thank you very much in advance!
The relatively straight forward (but not easy) step in creating a bridge between Apache/PHP and Tomcat/Laszlo is by setting up mod_jk2 connector .. which is well documented elsewhere (including discussions in this forum).
But dynamically scripting lzx content (using php) proves to be trickier.
I have got it to work after a fashion in some early testing.
The carto.net link (posted earlier) explains why an extended php extension is required (for generating svg images) .. but in fact you are right .. there is no need for an additional php extension as I suggested. (I have corrected my post).
The extension .psvg is used (in carto.net example) to differentiate between static and dynamic svg .. but in our Apache<->Tomcat scenario the lzx extension will only be used in the Tomcat lps-2.2 web app.
Basically, in my experiments so far, Tomcat acts as a client to the Apache/PHP server, and an LzURL link in laszlo webapp to Apache/PHP generates lzx script .. dynamically.
And conversely, Apache acts as client to Tomcat server.
Not exactly tight integration.
_____________________________________
But the problem is that this mod_jk2 solution requires users to have complete control
over their server(s) platform(s) and this may not be practical for most users deploying Laszlo applications on hosted Apache and Tomcat servers.
_____________________________________
I have trawled the web to find examples of PHP running in native mode on Tomcat.
There was very little reference (just one Apache site reference to compiling and installing PHP on Tomcat - not in mod_jk2 mode).
But in the last few days I unearthed this great little jewel:-
PHPServlet from http://tools.herberlin.de/phpservlet/index.shtml
this is a native java servlet (phpservlet.war) which can be installed as a Tomcat webapp.
...
[Note: Instructions below assume that you have installed PHP on Tomcat server (say into c:\php) and have tested phpinfo.php. If you have no Apache http server (normally used with PHP), test PHP installation first on your Tomcat server in command line mode.]
now ..
shutdown Tomcat
remove any PHP version of phpservlet.jar (same name which conflicts) - but only if you have been trying out PHP_Java.dll extension in PHP.
install PHPServlet (from herberlin.de) into webapps\phpservlet\
go to webapps\phpservlet\web.xml
and set the path to php executable .. installed on Tomcat server.
<init-param>
<param-name>php.executable</param-name>
<param-value>c:/php/php-cgi.exe</param-value>
</init-param>
[Note: the path must be to the CGI executable php-cgi.exe and not php.exe.]
Also (optionally) if you still wish to connect to Tomcat, from Apache server, via mod_jk2,
add this mapping to phpservlet app into workers2.properties:
# Map the herberlin.de phpservlet webapp to the Web server uri space
[uri:/phpservlet/*]
worker=ajp13:localhost:8009
info=Map the herberlin.de phpservlet webapp
Also ensure that phpservlet.jar is in your Java classpath.
To be sure, I copied phpservlet.jar from webapps\phpservlet\
into %TOMCAT_HOME%\common\libs\
and %TOMCAT_HOME%\common\libs\phpservlet.jar
was inserted in CLASSPATH in Environment.
...
Now reboot and startup Tomcat
To test Tomcat + PHP configuration
launch .. http://localhost:8080/phpservlet/phpinfo.php
You should see the PHPInfo report.
This is PHP running in Tomcat server!
To test Apache2 <mod_jk2> Tomcat + PHP - if you want to use this mode as well
restart Apache http server (in the example below Apache server runs on port 88,
but it could be your default port 80 or whatever port you have configured).
launch .. http://localhost:88/phpservlet/phpinfo.php
and again you should see the PHPinfo report.
[Note: use Ctrl + F5 in your browser to ensure that it is a fresh load and not a cached earlier load].
...
I think that PHPServlet (although still in early release) will be a very useful webapp to allow PHP scripting on Tomcat. Certainly it simplifies the whole PHP-LZX bridging process.
...
CURL
regarding the use of cURL in PHP ..
here is a cURLsnippet which I have been using.
<?php
// launch php script to generate lzx script
// setup your own paths, and ports
//
//
$ch = curl_init ("http://localhost:88/test.lzx/");
// open target *.lzx file to be generated on the fly
$fp = fopen ("c:\\\\Tomcat5\\\\webapps\\\\lps-2.2\\\\my-apps\\\\phptest\\\\box.lzx", "w");
curl_setopt ($ch, CURLOPT_FILE, $fp);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_exec ($ch);
curl_close ($ch);
fclose ($fp);
// cURL allows :-
// monitor web site attributes
// scrape content to place in *.lzx or other file
// used for extracting web content and scripting
?>
[Note: in the $fp = fopen line above when I placed escape character "\"in front of backward slashes in the file path it was lost in the posting (I guess because this is a php site). So as a workaround I had to escape both backward slashes to ensure php displays correctly.]
The idea is that the lzx script is generated by php .. then the content "scraped" as an lzx script .. then file copied into defined lzx location.
This php generated lzx file can then be "included" in a normal lzx webapp .. which is called after the generation.
I'm not sure if this will work in a multi-thread applications .. seems unlikely since it is just copying a generated file and might crack under load.
But it might be used to rescript Laszlo content from time to time (e.g. dynamic sizing of components unable to be changed by lzx scripting).
..
I'm continuing to further test these approaches with PHP.
(a) mod_jk2
(b) native phpservlet.jar
(c) possibly a hybrid
(d) using PHP with Ming to dynamically generate embedded SWF files
(e) SVG generation
scheidecker
10-20-2004, 10:49 AM
Thanks for further explanations. That is really nice.
The problem is that if you write the physical file instead of actually generating it on the fly as you can spit out html through php the app will not work properly for more than one user at the same time.
In that sense, the manual explains how to do that very briefly like this:
http://www.laszlosystems.com/lps/docs/lzx-developers-guide/lps-request-types.html
Preprocessed Requests
The LPS also supports using dynamic, server-side back-end processors (e.g., PHP, JSP) to generate LZX application source code before the LPS compiles and serves it. For URLs that end in .lzx for which the LPS can not find a matching source file on disk, the LPS will strip off the .lzx suffix and run attempt to get the content of the modified URL via an HTTP GET. The contents returned should be a properly formatted LZX file. The LPS will then compile and server an application based on the dynamically generated source code. For example see the hello.jsp file in the examples directory. This example generates a Laszlo application that contains information about the current time and session. You can see the generated LZX source at:
http://localhost:8080/lps-2.1.2/examples/hello.jsp.lzx?lzt=source
and the generated application at:
http://localhost:8080/lps-2.1.2/examples/hello.jsp.lzx
And that is what I would like to know how to make it. Does Laszlo sell any type of support that you could buy to learn this?
Thanks.
I did write a caveat that the cURL file write might crack under multi-threading ..
it is only an experiment .. to find out what is possible ..
yes .. I have tracked the preprocessed option ..
hello.jsp.lzx discussions (http://www.laszlosystems.com/developers/community/forums/search.php?action=showresults&searchid=13887&sortby=lastpost&sortorder=descending)
now that php is running on Tomcat I intend to dig deeper into getting that option to work.
Regarding paying for support services .. I'm not a Laszlo insider .. just a developer .. and it's more fun figuring out how to make it work.
Finally got it all to fit together and work!
I have just created and tested a preprocessed example of hello.php.lzx (along the lines of pre-processed request - hello.jsp.lzx).
The final links in the build chain ..
Rather than running PHPServlet (herberlin.de) in a separate webapp, I decided to integrate PHPServlet into the LPS-2.2 webapp .. merging files and sub-directories; and also (importantly) merging the two web.xml files into one, to integrate both lzx and php servlet definitions and mappings.
In fact, I created a new consolidated webapp called lps-php into which I merged lps-2.2 and phpservlet independent web apps.
I did this by simply copying content from the two existing webapps into the new webapp.
In merging content into a common webapp, only one file needs to be renamed to avoid file naming conflicts in the common root. Since there is an index.html in both applications I changed the phpservlet\index.html to index-php.html and left the lps-2.2\index.html as default to launch Laszlo.
I now have three running webapps .. for test purposes ..
webapps\lps-2.2\ .. for Laszlo and JSP applications only
webapps\phpservlet\ .. for PHP applications only
webapps\lps-php\ .. for Laszlo, PHP and JSP applications
I then adapted the two files hello.html and hello.jsp in lps-2.2\examples, to use php script instead of jsp script.
So I now have hello.html and hello.php (adapted from hello.html and hello.jsp).
In effect, this is now a hello.php.lzx preprocessing example which I run in http://localhost:8080/lps-php/myapps/hello.html .. with hello.php in the same directory.
I'll aim to document steps in getting to this point although it's all in this thread.
Opens up opportunities for dynamic scripting of Laszlo applications.
scheidecker
10-21-2004, 07:37 AM
Nice Job!
I thought that a hello.php.lzx would be more reasonable aproach too.
I really would like to see that. I would like to see the details of it.
In fact, if you like, I could host your howto in one of my servers to help others as well.
I did not have much time to play with that this week.
Hanjo
07-12-2006, 03:58 AM
d~l, im a beginner in laszlo and I wannna ask some sample codes with laszlo and php integration. links or just very simple codes. And is it necessary to run the TOMcat and apache in running the page? why not appache alone that lzx is already generated by php?
The above examples discuss running PHP on the tomcat server. Probably not what you are looking for.
Your easiest approach would be to compile your OpenLaszlo apps in SOLO mode (search for "SOLO" in Deployment Guide). Then use the compiled SWF files in a conventional Apache+PHP server.
There is an example in the forum for deploying OpenLaszlo+PHP+MySQL.
astor84
07-12-2006, 03:06 PM
Originally posted by d~l
The above examples discuss running PHP on the tomcat server. Probably not what you are looking for.
Your easiest approach would be to compile your OpenLaszlo apps in SOLO mode (search for "SOLO" in Deployment Guide). Then use the compiled SWF files in a conventional Apache+PHP server.
There is an example in the forum for deploying OpenLaszlo+PHP+MySQL. I agree. SOLO mode is far less complex overall. If you don't need the specific features provided by the presentation server, there's really no point in setting up mod_jk.
SOLO is the way to go.
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.