Front Controller Pattern

Magento Front Controller

Magento uses the Front Controller pattern for the following purposes:

  • Receiving and processing browser data, transferring it to other system elements;
  • Defining controller and proper action to perform using routes;
  • Displaying browser-requested data using blocks, templates and model data.

Front Controller main structure is stored in Core module:
Mage/Core/Controller/Varien/Front.

Let’s review the basic steps of Front Controller initialization.

The first call is performed in Mage_Core_Model_App constructor:

wherein getFrontController method is called. Before current request processing starts, Mage_Core_Controller_Varien_Front object is created and its initialization is called.

Let’s take a closer look at the init method:

Controller_front_init_before event is initially called:

It can be used to set the $routerInfo[‘disabled’] flag and to determine routes your way.

Next, routes, determined in config.xml in web/routes section (global configuration), are selected. At this application initialization stage, these are admin and standard sections, i.e. admin routing areas are determined:

(Mage_Core_Controller_Varien_Router_Admin) and standard (Mage_Core_Controller_Varien_Router_Standard):

Next, all active modules are processed and specified ones are added to their routes configurations:

Next, controller_front_init_routers event is called:

With the help of this event CMS module adds CMS routing area. We add our own routes as well.

Specified event:

method itself:

Default route is added after all (Mage_Core_Controller_Varien_Router_Default):

At the end we have routs processing order:

admin

standard

cms

default

Module route is determined in its config.xml

For a frontend:

Frontend and Admin routs should differ.

After routes are formed, current browser request processing route is being determined (dispatch).

Main application address is checked:

Basic variables are prepared and flag of route correspondence in false is set. It determines if search should be continued or finished.

Rewrite is chosen from the database for the current request:

If there is a match, the address, corresponding to this rewrite, is followed. This is the custom url mechanism.

We also loop through rewrite, determined in global/routes configuration section:

Rewrite controllers are determined here:

Next, corresponding controller is determined and required action is performed:

Match search among all the known routes $this->_routers is performed.

As soon as controller match is found, it is initialized and specified action or default action is performed (unless the action is determined directly in the request). Our later articles will explain routes as well as routes processing in greater detail.

As may be seen from the source code, the maximum number of attempts may not exceed 100.

Next, current request contents, including events before and after displaying setting, are prepared to be sent to the browser. Displaying can be prepared and changed in them correspondingly:

 

2 thoughts on “Front Controller Pattern

  1. Thanks for your personal marvelous posting! I truly
    enjoyed reading it, you are a great author.I will ensure that I bookmark your blog and will come back at some point.
    I want to encourage continue your great job, have a nice day!

  2. Hello my family member! I wish to say that this article is amazing, nice written and come
    with approximately all significant infos. I’d like to peer extra posts like this .

Leave a comment