Zend Framework: The Front Controller May 19, 2011Posted by Tournas Dimitrios in Zend Framework v1.10.
The Front Controller is the “hard – worker ” of an MVC application , it was designed to be lightweight, modular, and extensible . The Controller receives a request (usually a HTTP request ) and it response by returning the requested data ( if available) . Although the concept is simple , there is quite a bit going behind the scenes . This article will just outline the basic functionality of the Front Controller .
The Front Controller is not a monolithic block but is composed from many components , connected with each other like a chain . Of course the representation of a chain in OOP implementation is expressed by Classes . Each Class can be extended or even replaced by a custom Class , so that the functionality of the Front Controller can highly customised . By default the Front Controller is targeted to web applications ( web browsers ) , so we will “stick” with this functionality . Advanced users can transform the Front Controller to other services like : CLI , web- services (SOAP , REST ) , by extending or replacing each part ( Class ) .
The Front Controller beyond the task of instantiating and orchestrating it’s components , it dispatches events . These events can be captured by the other MVC components and they will behave accordingly . Each event has a unique name , so we can capture that specific event and implement functionality at specific timing of the Front Controller’s work-flow . See this graphical representation of the application work-flow and this practical example .
The default components ( Classes ) used by the Front Controller are :
- The Request Object : Zend_Controller_Request_HttP
- The Standard Router : Zend_Controller_Router_Rewrite
- The Dispatcher : Zend_Controller_Dispatcher_Standard
- The Response Object : Zend_Controller_Response_Http
- Plugin Broker : Zend_Controller_Plugin_Broker (that’s the only part that is not customizable)
The basic steps of the Front Controller functionality in the exact order :
- The Front Controller is instantiated and requested data is passed to it
- A request object is created
- Front Controller dispatches a ” Route Startup ” event
- The Router object reads information from the request object
- Front Controller dispatches a ” Route Shutdown” event
- Front Controller dispatches a ” DispatchLoopStartup” event
- The Dispatch loop is started
- Front Controller dispatches a ” PreDispatch” event
- The Dispatcher object calls the Action Controller
- The Action Controller writes data to the Response Object
- Front Controller dispatches a ” PostDispatch” event
- Is there other actions left to call -> repead the last 3 steps , else go to next step
- Front Controller dispatches a ” DispatchLoopShutdown ” event
- Front Controller send response back to the browser