Hooking up a Front Controller under Apache...

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Hooking up a Front Controller under Apache...

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you have access to the php.ini for it, auto_prepend :)
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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....
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mod_rewrite wouldn't lose those headers.. hmm...
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yeah... kinda iffy on the good part, but since this is basically Apache specific, you're basically fine.. :?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.....)
Post Reply