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:
- Collect all routers (\Mage_Core_Controller_Varien_Front::init method)
- Apply url rewrites (\Mage_Core_Controller_Varien_Front::dispatch method)
- Match exact router (\Mage_Core_Controller_Varien_Front::dispatch method)
- 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:
- Mage_Core_Controller_Varien_Router_Admin – admin area
- Mage_Core_Controller_Varien_Router_Standard – frontend area
- Mage_Cms_Controller_Router – fronted area (static CMS pages)
- Mage_Core_Controller_Varien_Router_Default – 404/no-route requests
Magento Webdesign
Take your online store to the next level with BelVG Magento Webdesign
Visit the pageThere are two main ways to add a new router:
1. Via config.xml web->routers path:
#app/etc/code/local/Vendor/Module/config.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<global> <default> <web> <routers> <my_router_name> <area>my_area_name</area> <class>Vendor_Module_Controller_Router</class> </my_router_name> </routers> </web> </default> </global> |
#app/etc/code/local/Vendor/Module/Controller/Router.php
1 2 3 4 5 6 7 |
class Vendor_Module_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract { public function match(Zend_Controller_Request_Http $request) { // URL matching code } } |
2. Using observers #app/etc/code/local/Vendor/Module/config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<global> <events> <controller_front_init_routers> <observers> <vendor_module_add_router> <type>singleton</type> <class>Vendor_Module_Model_Observer</class> <method>controllerFrontInitRouters</method> </vendor_module_add_router> </observers> </controller_front_init_routers> </events> </global> |
#app/code/local/Colin/Request/Model/Observer.php
1 2 3 4 5 6 7 8 9 |
class Vendor_Module_Model_Observer extends Mage_Core_Model_Abstract { public function controllerFrontInitRouters($observer) { $front = $observer->getEvent()->getFront(); $router = new Vendor_Module_Controller_Route()_Request_Controller_Router_First(); $front->addRouter(‘custom’, $router); } } |
#app/code/local/Colin/Request/Controller/Router.php
1 2 3 4 5 6 7 |
class Vendor_Module_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract { public function match(Zend_Controller_Request_Http $request) { // URL matching code } } |
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:
- 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.
- 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:
- 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. - 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 - 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). - 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.
Partner With Us
Let's discuss how to grow your business. Get a Free Quote.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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
namespace Vendor\Module\Controller; class Router implements \Magento\Framework\App\RouterInterface { /** * @var \Magento\Framework\App\ActionFactory */ protected $actionFactory; /** * Response * * @var \Magento\Framework\App\ResponseInterface */ protected $_response; /** * @param \Magento\Framework\App\ActionFactory $actionFactory * @param \Magento\Framework\App\ResponseInterface $response */ public function __construct( \Magento\Framework\App\ActionFactory $actionFactory, \Magento\Framework\App\ResponseInterface $response ) { $this->actionFactory = $actionFactory; $this->_response = $response; } /** * Validate and Match * * @param \Magento\Framework\App\RequestInterface $request * @return bool */ public function match(\Magento\Framework\App\RequestInterface $request) { //URL matching code } } |
2. Register your router via #app/code/Vendor/Module/etc/di.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> <type name="Magento\Framework\App\RouterList"> <arguments> <argument name="routerList" xsi:type="array"> <item name="vendor_module_router" xsi:type="array"> <item name="class" xsi:type="string">Vendor\Module\Controller\Router</item> <item name="disable" xsi:type="boolean">false</item> <item name="sortOrder" xsi:type="string">22</item> </item> </argument> </arguments> </type> </config> |
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
Take your online store to the next level with BelVG Magento Custom Development
Visit the page