PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

CHAPTER 12 ■ ENTERPRISE PATTERNS


be deployed across several points in your system. For this reason, it becomes easy for some parts of your
code to fall out of alignment with others. Of course, a first step might be to centralize common
operations into library code, but you are still left with the calls to the library functions or methods
distributed throughout your system.
Difficulty in managing the progression from view to view is another problem that can arise in a
system where control is distributed among its views. In a complex system, a submission in one view may
lead to any number of result pages, according to the input and the success of any operations performed
at the logic layer. Forwarding from view to view can get messy, especially if the same view might be used
in different flows.


Implementation


At heart, the Front Controller pattern defines a central point of entry for every request. It processes the
request and uses it to select an operation to perform. Operations are often defined in specialized command
objects organized according to the Command pattern.
Figure 12–4 shows an overview of a Front Controller implementation.


Figure 12–4. A Controller class and a command hierarchy


In fact, you are likely to deploy a few helper classes to smooth the process, but let’s begin with the
core participants. Here is a simple Controller class:


namespace woo\controller;


//...
class Controller {
private $applicationHelper;
private function __construct() {}


static function run() {
$instance = new Controller();
$instance->init();
$instance->handleRequest();
}

Free download pdf