What is a Front Controller and how is it implemented?
Front Controller refers to a design pattern where a single component in your application is responsible for handling all requests to other parts of an application. It centralizes common functionality needed by the rest of your application. Templating, routing, and security are common examples of Front Controller functionality. The benefit to using this design pattern is that when the behavior of these functions need to change, only a small part of the application needs to be modified.
In web terms, all requests for a domain are handled by a single point of entry (the front controller).
An extremely simple example of only the routing functionality of a front-controller. Using PHP served by Apache would look something like this. Most important step is to redirect all requests to the front controller:
.htaccess
RewriteEngine On
RewriteRule . /front-controller.php [L]
front-controller.php
<?php
switch ($_SERVER['REQUEST_URI']) {
case '/help':
include 'help.php';
break;
case '/calendar':
include 'calendar.php';
break;
default:
include 'notfound.php';
break;
}
PHP Front Controller implementation without singleton : conceptual question
Is there any real reason to create FrontController instances inside the app? If there is then go ahead. Otherwise I am a bit skeptical about this as it may complicate things later on. What I'm afraid of is someone using new FrontController instances when there is a much simpler way, either out of laziness or because they don't know any better. Once they find something that works, some people tend to keep doing it even if there's a better way. Never forget that you may have to work with people that are not as good as you.
Personally I would hide this or not allow it altogether. However I would keep it in mind for later releases. If you ever stumble on a case where it's the best option by all means add it to your "official" interface.
Don't forget that once you release a function into the wild, it's excruciatingly difficult to kill it off.
PHP Front Controller with MVC
I'm surprised that in those two long and informative previous answers nobody bothered to actually answer your question in the simplest way.
You want the __autoload()
function. You'll still have to define it somewhere in your code, but it can simply be added to a global header file and then you don't have to explicitly write an include for every class definition.
/* auto load model classes */
function __autoload($class_name) {
$filename = 'class.' . strtolower($class_name) . '.php';
$file = __SITE_PATH . '/_model/' . $filename;
if( file_exists($file) == false ) {
return false;
}
require($file);
}
FrontController implementation for JSP pages
I guess your only problem is that you need to read a XML file before each and request is processed.
If this is only the case than Filters are best suited.
But in case there are more that needs to be done and you really need a centralized control on all request than you can do anyone of the following: -
Struts (it doesn't matter that your
project is small or big, but using a
predefined and proven pattern is
always useful).In your Controller put a mapping of
logical URL's with Physical URL's
and now put your mapping to Logical
URL's and not the physical URL's.
Related Topics
"Vertical" Regex Matching in an Ascii "Image"
Is There Any Particular Difference Between Intval and Casting to Int - '(Int) X'
Using Default Arguments in a Function
Replace Last Occurrence of a String in a String
Passing an Array as Arguments, Not an Array, in PHP
How to Get the Current Page'S Full Url on a Windows/Iis Server
Which Is Faster in PHP, $Array[] = $Value or Array_Push($Array, $Value)
Checking to See If One Array'S Elements Are in Another Array in PHP
Can You Store a Function in a PHP Array
Convert Utf8-Characters to Iso-88591 and Back in PHP
Rebase Array Keys After Unsetting Elements
PHP - Merging Two Arrays into One Array (Also Remove Duplicates)
Adding Minutes to Date Time in PHP
How to Locate the PHP.Ini File (Xampp)
When to Use Strtr VS Str_Replace
Difference Between Method Calls $Model-≫Relation(); and $Model-≫Relation;