The Front Controller has an array of “routers” that it uses to decide which module the URL should trigger so Front Controller plays a very crucial role in Magento. And do you know how to locate Front Controller class, or events that Front Controller fires and Front Controller’s responsibilities? If you are interested in these issues, please follow my article.
I. Locate Front Controller class

– The position of the directory: app/code/core/Mage/Core/Controller/Varien/Front.php

– Receive all requests from browser and return HTML code.

– Front controller uses routes of the system to define the controller and the action that are called.

– Example: routers in the file config.xml of the Customer module.

<routers>
 <customer>
   <use>standard</use>
    <args>
          <module>Mage_Customer</module>
          <frontName>Customer</frontName>
    </args>
 </customer>
</routers> 

– Routers can receive 3 values for the bar including:

  • Standard (class Mage_Core_Controller_Varien_Router_Standard)
  • Admin (class Mage_Core_Controller_Varien_Router_Admin)
  • Default (class Mage_Core_Controller_Varien_Router_Default)

II -List all events that Front Controller fires
– controller_front_init_before (app/code/core/Mage/Core/Controller/Varien/Front.php)

  • ‘front’ => Mage_Core_Controller_Varien_Front

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

  • ‘front’ => Mage_Core_Controller_Varien_Front

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

  • ‘front’ => Mage_Core_Controller_Varien_Front

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

  • ‘front’ => Mage_Core_Controller_Varien_Front

III. Explain Front Controller’s responsibilities
– Directly receive the request from browser.

  • All requests call the function Mage_Core_Controller_Varien_Front::dispatch()

– URL rewriting process.
– Load the module and action controller corresponding with the requests through routers to process the requirements which are sent from clients.

  • Collect routers: admin, standard, default.
  • Use the function match() of routers to define the module and controller that are requested.
  • Call the function dispatch() of the controller requested.
  • Call the action requested.

– Return HTML for browser

  • Call the function sendResponse() of Mage_Core_Model_Response_Http (extend from Zend_Controller_Response_Abstract).
  • Call the function sendHeaders() – use the header() function of PHP.
  • Call the function outputBody() to show the whole content of the body part:
echo implode('', $this->_body);

Question: Where is the value of $this > _body receive from?
Answer: The variable $_body is an array() and Action controller added value to this variable.
For example: When you call $this > renderLayout() in Action controller:


$output = $this->getLayout()->getOutput();
Mage::getSingleton('core/translate_inline')
->processResponseBody($output);
$this->getResponse()->appendBody($output);

Well, the post ends here. If you have other problems, don’t hesitate to discuss with me by leaving comments below!
Thanks.

Author

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

2 Comments

  1. I am able to understand how routers are collected using collectRoutes() function. Then how magento tries to match router for the request using match() function.
    But how magneto router initialize the specific Action Controller and Action method ?
    What is the utility of controller_front_send_response_before and controller_front_send_response_after events?

    • Hi Suman,
      Magento router initializes the special Action Controller/Method by the match() method in the Standard Router. You can view the code in this file app\code\core\Mage\Core\Controller\Varien\Router\Standard.php
      Also, controller_front_send_response_before / after is the event dispatched before and after Magento sends the response to a browser (it is echo function).
      Feel free to leave comments if you have any other questions. I am glad to discuss with you. Nice day;)

Write A Comment