Article:
  Building a PHP Front Controller
Subject:   Why use a controller paradigm?
Date:   2004-08-04 14:37:30
From:   thomaskostecki
Response to: Why use a controller paradigm?

There are a few reasons that I've found that make front controllers better than the way you are doing. I'll give you the first two that popped into my head.


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