Before working on any platform , every Developer should know the MVC structure and Design Pattern of respective platform. Magento is built with an tremendous MVC architecture. In general, MVC architecture is divided into two patterns, Convention based and Configuration based.
Convention Based MVC:
In an Convention Based architecture, we need to create the files according to the system’s core rule. Then the system will pick it up automatically and starts working. CakePHP and Symphony are the best examples of Convention Based MVC.
Configuration Based MVC:
In an Configuration Based architecture, we need to create the files according to the system’s core rule and we should explicitly tell system that we have created new files. Magento is Configuration based MVC since it is built with Zend Framework.
Let me show how typical MVC works and how Magento is different. Let us take and example – http://www.example.com
Typical MVC Pattern:
In an typical MVC patterns, the Frontcontroller (i.e. index.php) will include the bootstrap file and autoloads the class required to run system. In our case, system doesn’t have the controller in URL. So the system will look for default controller defined obviously the Index controller will get loaded. Then the system will load the Index action defined in class and renders the Output.
Magento MVC Pattern:
Magento uses different approach when compared to typical MVC architecture. Lets see how magento identifies home page and renders html in frontend. Following are code dispatch method handled by Magento – same example – http://www.example.com.
Note: I used XDebug – a debugging tool commonly used for PHP. You can see the code execution flow at above image.
- A URL is intercepted by single PHP file(i.e. index.php)
- PHP file instantiates Magento Application (i.e. Mage::run()). Now the application will go to Mage_Core_Model_App.
- Application will look for Front Controller Object according to URL(using Mage_Core_Controller_Varien_Front class).
- Front Controller instantiates routers defined in global configuration(i.e. all module config.xml).This step is done through Mage_Core_Controller_Varien_Router_Standard class.
- Router will check for “match” in URL.In our case, there is no controller given in URL , so the application will find out the Controller by 4th step.This step is done through Mage_Core_Controller_Varien_Action class.
- Application then gives respective action found in URL. In our case, there is no action is given, so the application will give default action (i.e. Index action).This step is done through Mage_Core_Controller_Varien_Action class.
- Application then goes to the CMS controller which they found in 5th step and goes to the Index action found in 6th step. This step is done through Mage_Cms_Indexcontroller.
- The application will instantiate respective models, views and helpers according to request.
- Application then looks for layout files and gets output. This step is done through Mage_Core_Model_Layout class.
- The layout then called to render output of requested URL. This step is done through Mage_Core_Block_Template.
Finally you will see the Output rendered by the application. I hope some what I tried to give my understanding of How Magento MVC works?.Any suggestions/clarifications/corrections would be really helpful!