Page 1 of 1

Hooking up a Front Controller under Apache...

Posted: Thu Sep 01, 2005 8:14 pm
by nielsene
I'd like to start putting my new Front Controller through its paces.

I've been reading several sites about different ways to "register" the Front Controller script with Apache (1.3 family) and I'm not finding anything that is likely to work well.

My "requirements":
1. This front controller should be invoked for all *.php requests (I can't use a custom extension with my fallback code.)
2. The entire application is normally installed at the top level of the domain -- so no extra directory fake like a ForceType is possible.
3. I need the original URI available, preferrably not rewritten into a GET parameter.

1&2 seem to rule out the Handler/AddHandler based methods.
2 rules out the ForceType method

I don't know mod_rewrite, it might be able to work?

Posted: Thu Sep 01, 2005 8:29 pm
by feyd
If you have access to the php.ini for it, auto_prepend :)

Posted: Thu Sep 01, 2005 9:04 pm
by nielsene
Hmm, that might work. I'll play with it and see....

Hmm Looks like it would work for all but the virtual directories...

OK, maybe I need to give up on the legacy fallback, its causing enough problems with the FrontController's design, plus it greatly complicates hooking everything up. Hmm time to ponder things....

Posted: Thu Sep 01, 2005 9:50 pm
by nielsene
I was just testing out setting the custom 404 handler to the same script as auto-prepend; hoping it would catch the virtual directories. However it loses the REQUEST_URI and PATH_TRANSLATED then, so I can't get back to the request (not to mention the havoc it causes with the error logs.)

Any ideas how to make auto-prepend work with virtual directories and my above requirements?

Posted: Thu Sep 01, 2005 10:22 pm
by feyd
mod_rewrite wouldn't lose those headers.. hmm...

Posted: Thu Sep 01, 2005 10:27 pm
by nielsene
After some exaimination of the full contents of _SERVER, I can check for REDIRECT_ERROR_NOTES and extract the orginal filename from there, if set. So I think I can make this work... but I'm still not sure if its a good approach....

Posted: Thu Sep 01, 2005 10:41 pm
by feyd
yeah... kinda iffy on the good part, but since this is basically Apache specific, you're basically fine.. :?

Posted: Thu Sep 01, 2005 11:21 pm
by nielsene
Can you think of any way to avoid the use of the custom 404 handler, but still meet my requirements? Since the numberr of pages triggering the 404 is only going to grow as I migrate scripts to the new frameowrk.

I'm considering a mod_rewrite approach like:

Code: Select all

RewriteEngine On
RewriteRule ^BaseDir/(.*) BaseDir/FrontController.php [E=APP_REQUEST:$1]
In my production server there would be no basse dir, but in testing I tend to have mutliple branches installed at different locations.

Would there be any downside to using Mod_rewrite in this manner? I suspect it will limit the usefulness of my logs... but at least the error log wouldn't be getting cluttered.

Posted: Thu Sep 01, 2005 11:51 pm
by feyd
when I've done a 404 handling, I've used a code like

Code: Select all

RewriteCond ${REQUEST_FILENAME} !-d
RewriteCond ${REQUEST_FILENAME} !-s
RewriteCond ${REQUEST_FILENAME} !-l
RewriteRule .* /control.php
$_SERVER should have the original requesting URI in it..

Posted: Fri Sep 02, 2005 12:40 am
by nielsene
I seem to be settling on:

Code: Select all

RewriteRule ^/~nielsene/cib/main-dev(.*)([/|\.php])$ /~nielsene/cib/main-dev/FrontController.php [E=APP_REQUEST:$1$2,PT]
I needed the [/|\.php] to make sure index pages still worked and to stop it from messing with the CSS, images, and static XHTML.

While I could get the request out of REQUEST_URI, this helps make the rest of the code self-configuring. I have to configure the RewriteRule on the server no matter what, so embedding the path information here seems to make some sense.


I'll need to find a solution to fact that some of the subdirectories .htaccess files aren't exected now, but I'm sure I can a work-around (need to set custom include_paths, so ini_set will be my friend.....)