Front controllers are often used in web applications to implement workflows. While not strictly required, it is much easier to control navigation across a set of related pages (for instance, multiple pages used in an online purchase) from a front controller than it is to assign individual pages responsibility for navigation.
The front controller may be implemented as a Java object, or as a script in a scripting language such as PHP, Raku, Python or Ruby that is called for every request of a web session. This script would handle all tasks that are common to the application or the framework, such as session handling, caching and input filtering. Based on the specific request, it would then instantiate further objects and call methods to handle the required tasks.
The alternative to a front controller is the usage of page controllers mapped to each site page or path. Although this may cause each individual controller to contain duplicate code, the page-controller approach delivers a high degree of specialization.
Several web-tier application frameworks implement the front controller pattern:
Front controllers may divided into three components:
On the view side, the helper collects data and sometimes stores data as an intermediate station. Helpers do certain preprocesses such as formatting of the data to web content or providing direct access to the raw data. Multiple helpers can collaborate with one view for most conditions. Additionally, a helper works as a transformer that adapts and converts the model into a suitable format.
The front controller implemented in Java code:3
There are three primary benefits associated with the front controller pattern.4
The front controller pattern may incur performance issues because the single controller is performing a great deal of work, and handlers may introduce bottlenecks if they involve database or document queries. The front controller approach is also more complex than that of page controllers.
The page-controller pattern is an alternative to the front controller approach in the MVC model.
Fowler, Martin. "Front Controller". Retrieved September 26, 2017. /wiki/Martin_Fowler_(software_engineer) ↩
"Web MVC framework". Spring Framework Reference Documentation. Pivotal Software. Retrieved September 26, 2017. https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-servlet ↩
"Demo code in Java". Archived from the original on 2012-04-19.{{cite web}}: CS1 maint: bot: original URL status unknown (link) https://web.archive.org/web/20120419115929/http://java.sun.com/blueprints/patterns/FrontController.html ↩
"Benefits for using front controller". 17 March 2014. https://msdn.microsoft.com/en-us/library/ff648617.aspx ↩