tags:

views:

295

answers:

2

One of the things I like about Java servlets is the use of unobtrusive filters and interceptors. Basically you could use these things to enforce security, put extra information on the HttpRequest, do monitoring or whatever.

Is there some equivalent in PHP?

From what I've seen so far it seems that you tend to include a certain file in all your pages that will do things like start the session, enforce security, etc. Not as elegant. Is that the only solution?

+3  A: 

There isn't any servlet API equivalent provided with PHP, so the quick answer is you need to implement the logic yourself.

You have a lot of elbowroom on how you can accomplish this. The practice is to create a certain flow of control within your application by starting off with one file through which all requests are initially processed that will initialize some values, such as constants, file paths, etc, after which you start parsing the request and generate the requested content.

Obviously, it's quite quick to set up some main file and to parse the request. In the past we had the "one .php file to rule them all" practice taken to the max. You'd usually have a single file that would include the header, footer and based on some incoming GET or POST params you'd also include some other pages that would generate or display the content. You can easily understand how messy things could get!

My suggestion, before you go off and try to develop something yourself, is to try some of the existing frameworks that are already out there and start getting productive asap. I'll presume that PHP is not your primary expertise and that you're shopping for a framework that you can get up and running fast and quickly grasp it's complete scope. Two of them, CodeIgniter and CakePHP, are as frameworks to PHP as WordPress is to blogging - functional, simple and effective.

If frameworks don't follow suit with your likings, you should definitely read The no-framework PHP MVC framework.

kRON
+1  A: 

Nope, maybe a close call though.

There is a config directive to instruct php to include automagically a file in yours

auto_prepend_file  string

That can be useful for writing something similar.

You should peruse filter library embedded since php 5.2 (the last version available).
http://it.php.net/manual/en/filter.configuration.php. I've not used it already but it seems interesting.

By the way the framework hint is a valuable one. Though I don't like them (often I've to fight the framework and not the problem I had assigned).

Eineki