All about Magento E-commerce Store.......MagentoForum: hello world
Showing posts with label hello world. Show all posts
Showing posts with label hello world. Show all posts

Friday, September 16, 2011

Magento Controller Dispatch and Hello World


The Model-View-Controller (MVC) architecture traces its origins back to the Smalltalk Programming language and Xerox Parc. Since then there have been many systems that describe their architecture as MVC. Each system is slightly different, but all have the goal of separating data access, business logic and user-interface code from one another.
The architecture of most PHP MVC frameworks will look something like this.
  1. A URL is intercepted by a single PHP file (usually called a Front Controller)
  2. This PHP file will examine the URL, and derive a controller name and an action name (a process that's often called routing)
  3. The derived controller is instantiated
  4. The method name matching the derived action name is called on the controller
  5. This action method will instantiate and call methods on models, depending on the request variables
  6. The action method will also prepare a data structure of information, which is passed on to the view
  7. The view then renders HTML, using the information in the data structure it has received from the controller
While this pattern was a great leap forward from the "each php file is a page" pattern established early on, for some software engineers, it's still a hack. Common complaints are
  • The Front Controller PHP file still operates in the global namespace
  • Convention over configuration leads to less modularity.
    • URLs routing is often inflexible
    • Controllers are often bound to specific views
    • Even when a system offers a way to override these defaults, the convention leads to applications where it's difficult/impossible to drop in new a new model, view, or controller implementation without massive re-factoring.
As you've probably guessed, the Magento team shares this world view and has created a more abstract MVC pattern that looks somethinglike this.
  1. A URL is intercepted by a single PHP file
  2. This PHP file instantiates a Magento application
  3. The Magento application instantiates a Front Controller object
  4. Front Controller instantiates any number of Router objects (specified in global config)
  5. Routers check the request URL for a "match"
  6. If a match is found, an Action Controller and Action are derived
  7. Action Controller is instantiated and the method name matching the Action Name is called
  8. This action method will instantiate and call methods on models, depending on the request
  9. This Action Controller will then instantiate a Layout Object
  10. This Layout Object will, based on some request variables and system properties (also known as "handles"), create a list of Block objects that are valid for this request
  11. Layout will also call an output method on certain Block objects, which start a nested rendering (Blocks will include other Blocks)
  12. Each Block has a corresponding Template file. Blocks contain PHP logic, templates contain HTML and PHP output code
  13. Blocks refer directly back to the models for their data. In other words, the Action Controller does not pass them a data structure