Magento Front Controller

The Magento front controller is first noticed in the Mage_Core_Model_App class. Specifically, the line below.


1. The first step is to initialise the Magento front controller using the _initFrontController() method. initFrontController() will then instantiate a new class of Mage_Core_Controller_Varien_Front. The init() method of this class is then called.

$this->_frontController = new Mage_Core_Controller_Varien_Front();

Note that the Mage_Core_Controller_Varien_Front class is the parent class for both Mage_Core_Controller_Front_Action and Mage_Adminhtml_Controller_Action, which in turn are the parent classes when you add a custom frontend or adminhtml router.

So we arrive at the init() method. Firstly, the routers are collected by Magento in an array looking in the config tree for the routers under the web/routers node in Magento’s configuration xml. There are two routers that are configured this way, which are the standard router (Mage_Core_Controller_Varien_Router_Standard) and the admin router (Mage_Core_Controller_Varien_Router_Admin).

You can see this within the config.xml file of the Mage_Core module.


There are two other routers that haven’t been added yet, however we will come onto these shortly.

So we foreach around the routers array and instantiate a new class of both of them. We then actually collect all of the routers that use the standard or admin routers using the collectRoutes() method.

It’s worth noting that the collectRoutes() method in Mage_Core_Controller_Varien_Router_Admin does reference the parent::collectRoutes() method in the Standard class. The collectRoutes() method in the admin checks to see if a custom admin URL is being used for Magento.

So looking at the Mage_Core_Controller_Varien_Router_Standard class’ collectRoutes() method, we collect the web routers. Some of the admin routes may use the traditional config, and some may use the updated config (with the before/after tags). This is checked within this function and the routers get ordered specifically under this the below if conditional.

if ($routerConfig->args->modules) {

At the end of this function, the addModule() method gets used. This adds the module name and the router name to array properties.

So heading back to the init() function of Mage_Core_Controller_Varien_Front, an event is dispatched called controller_front_init_routers. If we have a look at an observer that listens to this event, we can see a function called initControllerRouters() is called.


This function is located in Mage_Cms_Controller_Router. All this does is add the third router, the CMS Router to the configuration tree.

public function initControllerRouters($observer) {
    /* @var $front Mage_Core_Controller_Varien_Front */
    $front = $observer->getEvent()->getFront();

    $front->addRouter('cms', $this);

Lastly, that leaves the Default router. This router is hardcoded in and gets instantiated and added Mage_Core_Controller_Varien_Router_Default.

// Add default router at the last
$default = new Mage_Core_Controller_Varien_Router_Default();
$this->addRouter('default', $default);

Note: This article is based on Magento Community/Open Source version 1.9.