When there is a request in browser (URL), first Magento uses Request routing to analyze URL and finds out the suitable code to match that request. And then, it defines Controller or Action to return Response. So, Request routing helps you coordinate activities in Magento. You can learn more with my article below.
I. Request routing/flow in Magento

Request flow in Magento:

– Request URL

– index.php (Mage::run())

– app/Mage.php (Mage::app()->run())

– app/code/core/Mage/Core/Model/App.php

+ Install module database (Mage_Core_Model_Resource_Setup::applyAllDataUpdates())

+ Init and Dispatch controller ($this->getFrontController()->dispatch());

– app/code/core/Mage/Core/Controller/Varien/Front.php

+ Chose router match for dispatch ($router->match($this->getRequest()))

– app/code/core/Mage/Core/Controller/Varien/Router/Admin.php

app/code/core/Mage/Core/Controller/Varien/Router/Standard.php

app/code/core/Mage/Core/Controller/Varien/Router/Default.php

+ dispatch Action ($controllerInstance->dispatch($action);)

– app/code/core/Mage/Core/Controller/Varien/Action.php

+ Call Action function (Example: indexAction())

– custom Controller/Action

– (app/code/core/Mage/Core/Controller/Response/Http.php)

Request routing in Magento:

– Init router before using: Mage_Core_Controller_Varien_Front::init()

  • Init Router uses configuration file with configuration path: default/web/routers
  • Init the custom router by event controller_front_init_before, controller_front_init_routers
  • Init the default router

– Try to select router: Mage_Core_Controller_Varien_Front::dispatch()

  • Function match() of router will chose match controller/action to call. ($controllerInstance->dispatch($action);)
    • Predispatch
    • Call Controller Action Method
    • Postdispatch
  • Router will try to select router maximum – 100 times.

– Dispatch to controller/action (Example: indexAction())

II. Create module with controller

– Register module by file /app/etc/modules/Magestore_Test.xml

– Configure module /app/code/local/Magestore/Test/etc/config.xml, register router for controller can be used.

– Create controller /app/code/local/Magestore/Test/controllers/IndexController.php

III.  Override an existing controller

Step 1: Add configure in config.xml file

1. First choice:

Run at \app\code\core\Mage\Core\Controller\Varien\Front.php, function dispatch()

– from: expression to controller link

– to: controller to rewrite

– complete: choose override complete layout handle or controller only

2. Second choice:

Run at app\code\core\Mage\Core\Controller\Varien\Router\Standard.php, function collectRouters()

3. Third choice:

Run at app\code\core\Mage\Core\Controller\Varien\Action.php, function _rewrite()

3.1 Rewrite all actions

3.2. Rewrite each action

Step 2: Write controller file.

Nice code. 😀

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

1 Comment

  1. Vishal Lakhani Reply

    Very Useful post .. Thanks for sharing this. it will be helpfull for understanding magento flow.hope it will help me to preparing for magento certificate exam

Write A Comment