| Article: |
Building a PHP Front Controller | |
| Subject: | Why use a controller paradigm? | |
| Date: | 2004-07-14 00:25:36 | |
| From: | gaeldesign | |
|
I don't want to sound like I'm criticizing this article, because I'm not. The "front controller" design pattern is well-known in Web app circles. But, from my perspective, I have never understood any reason to use it. No article I've read on the matter, and no PHP app I've inspected, has shown me any good reason for it. I've seen good PHP apps that don't use it (even while being very OO in nature), and my own apps don't use it. Honestly, what's the point? The argument seems to be that you can run the same kind of "set up the app framework" stuff on every page. But, er, that can be done anyway. See here in a fake code example:
|
||
Showing messages 1 through 1 of 1.
-
Why use a controller paradigm?
2004-08-04 14:37:30 thomaskostecki [View]



One: You gave this example.
"page2.php"
$myapp = new MyWebApp();
$myapp->setupWhateverYouNeed();
// program logic
Page2LogicGoesHere();
// include page template
includePage2Template();
// Optional: clean up code
$myapp->cleanUp();
---------------------------------------
What if this structure needed to change for some reason? Lets say you now need to do
$myapp = new MyWebApp($someParam);
instead of
$myapp = new MyWebApp();
You now need to change this in many places instead of just one!
Two:
php paths are based on the file in the url. If you use your example of
http://www.mydomain.com/page1.php
http://www.mydomain.com/subfolder/page2.php
then it gets really complicated. The relative paths of all the requires in all the files used in page2.php will need to be different than all the requires for all the files used in page1.php. There are ways around this, but I think it's cleaner to use a controller