Get Free Audit

Front Controller Pattern

Oct 24, 2012

1924 Andrey Litvin

Front Controller Pattern

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.

Magento 2 Development

Take your online store to the next level with BelVG Magento 2 Development

Visit the page

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:

Andrey_Dubina
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey


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.

Andrey_Dubina

Partner With Us

Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey


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:


magento custom development

Magento Custom Development

Take your online store to the next level with BelVG Magento Custom Development

Visit the page
Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

8 Comments

  1. Nofel,

    I mentioned how to disable admin routers just as an experiment. The routers were disabled through event – to repeat it, you need to create a module where you should define the event that was described in the article. But it is not recommended to disable admin routers because in this case the admin part of Magento will not work . Most probably your problem was not caused by disabling routers. We need more details to find out what happened and to be able to answer your question.

  2. I am totally new to magento and i have a router error while my host upgraded the server and now i am stuck.I see you have mentioned how to disable routers by
    public function controller_front_init_before(Varien_Event_Observer $observer)
    {
    //Disable admin
    Mage::app()->getStore()->setConfig(‘web/routers/admin/disabled’, ‘disabled’);

    //Disable standard
    Mage::app()->getStore()->setConfig(‘web/routers/standard/disabled’, ‘disabled’);
    }
    but where and how to place it. pls guide me

  3. Diogo Santiago,

    You can create the event using code similar to the following piece:

    Parameters would rather be specified with local.xml, config.xml files.

  4. Nice Article… How can i use controller_front_init_before to disable routers ? The routerInfo variable is created after this event is dispatched….

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *