Front controller
The front controller software design pattern is listed in several pattern catalogs and related to the design of web applications. It is "a controller that handles all requests for a website", which is a useful structure for web application developers to achieve the flexibility and reuse without code redundancy.
Instruction
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 from a front controller than it is to make the individual pages responsible for navigation.The front controller may be implemented as a Java object, or as a script in a script language like PHP, Raku, Python or Ruby that is called on every request of a web session. This script, for example an index.php, 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 particular task required.
The alternative to a front controller would be individual scripts like login.php and order.php that would each then satisfy the type of request. Each script would have to duplicate code or objects that are common to all tasks. However, each script might also have more flexibility to implement the particular task required.
Examples
Several web-tier application frameworks implement the front controller pattern, among them:- Apache Struts
- ASP.NET MVC
- Cairngorm framework in Adobe Flex
- Cro or Bailador frameworks in Raku
- Drupal
- MVC frameworks written in PHP. For example, Yii, CakePHP, Laravel, Symfony, CodeIgniter and Zend Framework
- Spring Framework
- Yesod written in Haskell
Implementation
- XML Mapping: files that map requests to the class that will handle the request processing.
- Request Processor: used for dealing with the request processing.
- Flow Manager: first get the request and the output of the processing, then determine what will show on the next page.
Participants and responsibilities
Demo implementation in Java
Here is part of a demo code to implement front controller.private void doProcess
throws IOException, ServletException
Benefits and liabilities
There are three benefits for using front controller pattern.- Centralized control. Front controller handles all the requests to the web application. This implementation of centralized control that avoids using multiple controllers is desirable for enforcing application-wide policies such as users tracking and security.
- Thread-safety. A new command object arises when receiving a new request and the command objects are not meant to be thread safe. Thus, it will be safe in the command classes. Though safety is not guaranteed when threading issues are gathered, codes that act with command is still thread safe.
- Configurability. Since only one front controller is needed in web application, the configuration of web applications implementation is largely simplified. The handler accomplishes the rest of dispatching so that it is not required to change anything before adding new commands with dynamic ones.
Relationship with MVC pattern
- In order to improve system reliability and maintainability, duplicated codes should be avoided and centralized when they are of the same common logic through the whole system.
- The data for the application is better to be handled in one location, thus there will be no need to duplicate database retrieval code.
- Different roles in the MVC pattern should be separated to increase testability, which is also true for controller part in the MVC pattern.
Comparison
Page Controller | Front Controller | |
Base class | Base class is needed and will grow simultaneously with the development of the application. | The centralization of solving all requests is easier to modify than base class method. |
Security | Low security because various objects react differently without consistency. | High. The controller is implemented in coordinated fashion, making the application safer. |
Logical Page | Single object on each logical page. | Only one controller handles all requests. |
Complexity | Low | High |