NYCPHP Meetup

NYPHP.org

[nycphp-talk] Many pages: one script

Hans Zaunere lists at zaunere.com
Sun Aug 5 14:42:26 EDT 2007



Elliotte Harold wrote on Sunday, August 05, 2007 1:43 PM:
> I'm considering a simple site that I may design in PHP. PHP is
> probably the simplest solution except for one thing: it carries a
> very strong coupling between pages and scripts. As far as I've ever
> been able to tell PHP really, really, really wants there to be a
> single primary .php file for each URL that does not contain a query
> string (though that file may of course invoke others).

PHP doesn't actually care, but...

> For the system I'm designing that simply won't work. In Java servlet
> environments it's relatively trivial to map one servlet to an entire
> directory structure, so that it handles all requests for all pages
> within that hierarchy.

It has to do with the way PHP reaches into the request processing stack in
Apache (assuming Apache).  Basically PHP doesn't reach as far up the request
stack as other things do, like mod_perl, Java, etc, which of course could be
argued as a good or bad thing.

> Is there any *reasonable* way to do this in PHP? The only way I've
> ever seen is what WordPress does: use mod_rewrite to redirect all
> requests within the hierarchy to a custom dispatcher script that
> converts actual hierarchy components into query string variables. I
> am impressed by this hack, but it's way too kludgy for me to be
> comfortable with. For one thing, I don't want to depend on
> mod_rewrite if I don't have to.

A lot of people use mod_rewrite, but I never was a big fan either.  However,
you can implement this "fuse-box" style processing quite elegantly in pure
Apache.  There are a number of ways, most of which are covered in these
results:

http://www.google.com/search?q=mediawiki+url+rewrite

There are other options as well, including the ErrorDocument hack and
playing with ForceType, but I'm not much of a fan of those either.  I find
the following to be the most elegant:

    Alias /support/ "/var/www/www.something.com/support/"
    Alias /Test/ "/var/www/www.something.com/Test/"
    AliasMatch /(.*) "/var/www/www.something.com/index.php"

/support becomes a nice place to throw static stuff, like images, CSS, etc.

/Test can be used as a test bed, to test PHP scripts outside of the
fuse-box.

And then index.php is where the action is, getting called on every request.
Of course, the above can be adjusted as needed to have an unlimited number
of fuse-boxes at different URLs, etc.  Combined with things like Apache's
AddType, the possibilities are endless.  I think you can even use AddType
for a directory (or maybe it's ForceType).

I actually leave index.php empty, and use auto_prepend_file to call a PHP
file that handles the heavy lifting.  This typically allows for better
delegation of responsbility and keeping PHP code outside of the
DocumentRoot.

And all of the above can be combined with various combinations of
<Directory> and <Location> directives in Apache, making it really flexible
and dizzying.

But, I generally keep it simple and use something like the above, and then
have a request processor in PHP do the URL mapping in a style akin to the
Java world.  Straightforward, none of the PATH_INFO confusions, and
setup-and-forget.

---
Hans Zaunere / President / New York PHP
    www.nyphp.org  /  www.nyphp.com





More information about the talk mailing list