John Lindal’s Blog
Struts for PHP
March 17, 2009 on 7:50 pm | In Programming | No CommentsIt’s been about two years since I switched from PHP to JSP/Java. While certainly not the only game in town, Struts2 is pretty nice. For a Java solution, that is
What I can’t understand is why so many people want to port such frameworks to PHP. (Rasmus always rails against frameworks — no pun intended.) In Java, the basic functionality is, Write a Servlet which serves a page using
Compared to that, configuring Actions and JSP’s via XML is a pleasure. But in PHP, MVC is as simple as putting the PHP code at the top of the file and the HTML below it. I keep them together since they are tightly coupled. If you don’t like to mix them, put the HTML in an println()..inc file and include it at the end of the PHP code. If you need to serve different pages based on state, e.g. a workflow, a switch statement at the end of the PHP code can load the appropriate .inc file.
The only concept from Struts that isn’t directly available in PHP is the interceptor stack. Luckily, this is easily implemented. First, set up a mod_rewrite rule:
RewriteRule ^(.+)\.php /interceptor.php?target=$0 [QSA]
Then build interceptor.php to execute your interceptor stack before including the target page. If you want to configure which interceptors to execute based on the target, use an XML config file. Bonus points for parsing it once and storing the results in APC to speed up execution!
Update: A friend pointed out the auto_prepend_file directive. Combine this with the information in $_SERVER, and you may not need to use mod_rewrite!
No Comments yet »
RSS feed for comments on this post.
Leave a comment
You must be logged in to post a comment.
Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds.
Valid XHTML and CSS. ^Top^