ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
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:


"page1.php"


$myapp = new MyWebApp();
$myapp->setupWhateverYouNeed();


// program logic
Page1LogicGoesHere();


// include page template
includePage1Template();


// Optional: clean up code
$myapp->cleanUp();


"page2.php"


$myapp = new MyWebApp();
$myapp->setupWhateverYouNeed();


// program logic
Page2LogicGoesHere();


// include page template
includePage2Template();


// Optional: clean up code
$myapp->cleanUp();


Honestly, there's no reason you'd have to duplicate any code other than a very small handful of lines in each file. The separate objects in their own code files would handle 99% of the work.


The benefit of this approach is that you can use real files for real URLs. Using the above example, you get:


http://www.mydomain.com/page1.php
http://www.mydomain.com/subfolder/page2.php


And so on. Real pages! Real files! No fake stuff. I hate fake stuff!


So there you go. Maybe there's something I've overlooked, but I can't think of anything. Like I said, no Web app I've come across seems like it HAS to use the front controller paradigm to be easy to design and manage.


Regards,


Jared


1 to 1 of 1
1 to 1 of 1