Get Free Audit

Front Controller in Magento 1 and 2 (Magento Developer Certification)

Aug 17, 2017

1009 Andrey Litvin

Front Controller in Magento 1 and 2 (Magento Developer Certification)

Magento 1 uses Front Controller Pattern implemented in \Mage_Core_Controller_Varien_Front class. Magento Front Controller instantiates just after establishing database connections, loading configuration and executing other common routines at the end of \Mage_Core_Model_App::run method.

There are four main responsibilities of the Front Controller:

  1. Collect all routers (\Mage_Core_Controller_Varien_Front::init method)
  2. Apply url rewrites (\Mage_Core_Controller_Varien_Front::dispatch method)
  3. Match exact router (\Mage_Core_Controller_Varien_Front::dispatch method)
  4. Send response from matching router back to the client. (\Mage_Core_Controller_Varien_Front::dispatch method)

Magento 1 Router Classes

By default Magento 1 contains four router classes which are loaded in the following sequence:

  1. Mage_Core_Controller_Varien_Router_Admin – admin area
  2. Mage_Core_Controller_Varien_Router_Standard – frontend area
  3. Mage_Cms_Controller_Router – fronted area (static CMS pages)
  4. Mage_Core_Controller_Varien_Router_Default – 404/no-route requests
magento webdesign

Magento Webdesign

Take your online store to the next level with BelVG Magento Webdesign

Visit the page

There are two main ways to add a new router:

1. Via config.xml web->routers path: 

#app/etc/code/local/Vendor/Module/config.xml


#app/etc/code/local/Vendor/Module/Controller/Router.php


2. Using observers #app/etc/code/local/Vendor/Module/config.xml


#app/code/local/Colin/Request/Model/Observer.php


#app/code/local/Colin/Request/Controller/Router.php

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


The main difference between these methods is the order our router will be added. When using web->routers config path your router will be added right after admin and standard routers added this way in method \Mage_Core_Controller_Varien_Front::init  

If you would prefer to use observers to add your router then there are actually two events that will allow you to do this:

  1. controller_front_init_before – dispatched at first line in Front Controller’s init method and will allow you to insert your router just before admin and standard routers.
  2. controller_front_init_routers – dispatched just after adding routers from web->routers config path and will add your router before default router which is loaded explicitly at the end of \Mage_Core_Controller_Varien_Front::init method.

Magento 1 Front Controller Events

There are four events dispatched in Magento 1 Front Controller:

  1. controller_front_init_before
    Dispatched at the first line of \Mage_Core_Controller_Varien_Front::init method and can be used to add your own router before Magento Core routers.
  2. controller_front_init_routers
    Dispatched right after Magento finished adding routers from web->routers config path and before adding Default router. By default, this event is used to load Magento CMS router. Can be used to insert routers here or alter routers order/availability
  3. controller_front_send_response_before
    Dispatched in \Mage_Core_Controller_Varien_Front::dispatch method before rendering and sending output to the browser. Can be used to launch something before sending output (allow cookie setting).
  4. controller_front_send_response_after
    Dispatched in \Mage_Core_Controller_Varien_Front::dispatch method after sending output to the browser and can be used to perform maintenance task like syncing additional info with database.
Andrey_Dubina

Partner With Us

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

Magento 2 Front Controller

In the same manner as in Magento 1 Front Controller is an entry routing point which is responsible for finding matching router for the current request. Magento 2 Front Controller must implement \Magento\Framework\App\FrontControllerInterface and is loaded via automatic constructor dependency injection in \Magento\Framework\AppInterface::launch method.

You should note that Magento 2 has several classes implementing this interface each of them corresponds to different output formats (default, soap, rest). While serving the same goal of matching correct router and dispatching the corresponding controller as in Magento 1, Magento 2 Front Controller provides the more sophisticated way of collecting and extending default router list.

Creating custom router

First of all, Magento 2 routers must implement \Magento\Framework\App\RouterInterface.

They are collected via automatic constructor dependency injection in Front Controller class: \Magento\Framework\App\FrontController.

In order to create your own custom router for Magento 2 you need to:

1. Create router class #app/code/Vendor/Module//Controller/Router.php


2. Register your router via #app/code/Vendor/Module/etc/di.xml


If your router will handle only requests from one area (frontend or adminhtml) then you should use different path for di.xml file:

#app/code/Vendor/Module/etc/frontend/di.xml or #app/code/Vendor/Module/etc/adminhtml/di.xml depending on the chosen area. This config also provides a way to select your router order (sortOrder item) without tinkering with events like in Magento 1.

Check out the article Controllers, Routers and Responses in Magento 2 to learn more about Front controllers in Magento 2.

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

Post a new comment

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