Front Controller
   HOME

TheInfoList



OR:

The front controller
software design pattern In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code ...
is listed in several pattern catalogs and is related to the design of web applications. It is "a
controller Controller may refer to: Occupations * Controller or financial controller, or in government accounting comptroller, a senior accounting position * Controller, someone who performs agent handling in espionage * Air traffic controller, a person ...
that handles all requests for a
website A website (also written as a web site) is a collection of web pages and related content that is identified by a common domain name and published on at least one web server. Examples of notable websites are Google Search, Google, Facebook, Amaz ...
," which is a useful structure for web application developers to achieve flexibility and reuse without code redundancy.


Instruction

Front controllers are often used in
web application A web application (or web app) is application software that is accessed using a web browser. Web applications are delivered on the World Wide Web to users with an active network connection. History In earlier computing models like client-serve ...
s 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 Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
object, or as a script in a
scripting language A scripting language or script language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. Scripting languages are usually interpreted at runtime rather than compiled. A scripting ...
such as
PHP PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group. ...
, Raku,
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
or
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sa ...
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.


Examples

Several web-tier application frameworks implement the front controller pattern: *
Apache Struts Apache Struts 2 is an open-source web application framework for developing Java EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt a model–view–controller (MVC) architecture. The WebWork framework ...
*
ASP.NET MVC ASP.NET MVC is a web application framework developed by Microsoft that implements the model–view–controller (MVC) pattern. It is no longer in active development. It is open-source software, apart from the ASP.NET Web Forms component, which i ...
* Cairngorm framework in
Adobe Flex Apache Flex, formerly Adobe Flex, is a software development kit (SDK) for the development and deployment of cross-platform rich web applications based on the Adobe Flash platform. Initially developed by Macromedia and then acquired by Adobe System ...
* Cro or Bailador frameworks in Raku *
Drupal Drupal () is a free and open-source web content management system (CMS) written in PHP and distributed under the GNU General Public License. Drupal provides an open-source back-end framework for at least 14% of the top 10,000 websites worldwide ...
*
MVC MVC may refer to: Science and technology * Maximum-value composite procedure, an imaging procedure * Multivariable calculus, a concept in mathematics * Multivariable control, a concept in process engineering * Mechanical vapor compression, a desal ...
frameworks written in
PHP PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group. ...
, such as
Yii Yii is an open source, object-oriented, component-based MVC PHP web application framework. Yii is pronounced as "Yee" or i:and in Chinese it means "simple and evolutionary" and it can be an acronym for "Yes It Is!". History Yii started ...
,
CakePHP CakePHP is an open-source web framework. It follows the model–view–controller (MVC) approach and is written in PHP, modeled after the concepts of Ruby on Rails, and distributed under the MIT License. CakePHP uses well-known software engineeri ...
,
Laravel Laravel is a free and open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern and based on Symfony. Some of the features ...
, Symfony,
CodeIgniter CodeIgniter is an open-source software rapid development web framework, for use in building dynamic web sites with PHP. Popularity CodeIgniter is loosely based on the popular model–view–controller (MVC) development pattern. While controller c ...
and
Laminas Laminas Project (formerly Zend Framework or ZF) is an open source, object-oriented web application framework implemented in PHP 7 and licensed under the New BSD License. The framework is basically a collection of professional PHP-based pack ...
*
Spring Framework The Spring Framework is an application framework and inversion of control container for the Java platform. The framework's core features can be used by any Java application, but there are extensions for building web applications on top of the Java ...
*
Yesod Yesod (Hebrew: יְסוֹד ''Yəsōḏ'', Tiberian: ''Yăsōḏ'', "foundation") is a sephirah or node in the kabbalistic Tree of Life, a system of Jewish philosophy. Yesod, located near the base of the Tree, is the sephirah below Hod and ...
, written in
Haskell Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lan ...


Implementation

Front controllers may divided into three components: #
XML Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable ...
mapping: files that map requests to the class that will handle the request processing. # Request processor: used for request processing and modifying or retrieving the appropriate model. # Flow manager: determines what will be shown on the next page.


Participants and responsibilities


Java implementation example

The front controller implemented in Java code: private void doProcess(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { ... try { getRequestProcessor().processRequest(request); getScreenFlowManager().forwardToNextScreen(request, response); } catch (Throwable ex) { String className = ex.getClass().getName(); nextScreen = getScreenFlowManager().getExceptionScreen(ex); // Put the exception in the request request.setAttribute("javax.servlet.jsp.jspException", ex); if (nextScreen

null) { // Send to general error screen ex.printStackTrace(); throw new ServletException("MainServlet: unknown exception: " + className); } }


Benefits and liabilities

There are three primary benefits associated with the front controller pattern. * Centralized control. The front controller handles all the requests to the
web application A web application (or web app) is application software that is accessed using a web browser. Web applications are delivered on the World Wide Web to users with an active network connection. History In earlier computing models like client-serve ...
. This implementation of centralized control that avoids using multiple controllers is desirable for enforcing application-wide policies such as user 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, code that interacts with commands is still thread-safe. * Configurability. As only one front controller is employed in a web application, the application configuration may be greatly simplified. Because the handler shares the responsibility of dispatching, new commands may be added without changes needed to the code. 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.


Relationship with MVC

# In order to improve system reliability and maintainability, duplicate code should be avoided and centralized when it involves common logic used throughout the system. # The data for the application is best handled in a single location, obviating the need for duplicate data-retrieval code. # Different roles in the model-view-controller (MVC) pattern should be separated to increase testability, which is also true for the controller part in the MVC pattern.


Comparison

The page-controller pattern is an alternative to the front controller approach in the MVC model. {, class="wikitable" ! !Page Controller !Front Controller , - , Base class , A base class is needed and will grow simultaneously with the development of the application. , The centralization of requests is easier to modify than is a base class. , - , Security , Low security because various objects react differently without consistency. , High, because the controller is implemented in a coordinated fashion. , - , Logical Page , Single object on each logical page. , Only one controller handles all requests. , - , Complexity , Low , High


See also

*
Design pattern (computer science) In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code ...
. *
Mediator pattern In software engineering, the mediator pattern defines an object that encapsulates how a set of objects interact. This pattern is considered to be a behavioral pattern due to the way it can alter the program's running behavior. In object-oriented ...
(note: the front controller pattern is a specialized kind of mediator pattern)


References


Notes

* *


External links


Bear Bibeault's Front Man™
a lightweight Java implementation. {{Design Patterns patterns Architectural pattern (computer science) Software design patterns