Can someone please explain this?
Let me tell you what i know. If the first three points are good, please explain the 4 point.
- Request come to controller.
- In Controller Action we initiate Models.
- Models collects or generates all the information needed by connecting to database etc.
What happens after that?
-
How do models transfer data to Blocks, or do Blocks get data from models?
-
Templates get the prepared data and show on the screen
- Also, does the request ever goes back to controller again?
Please explain. I’m confused at several places.
Nothing transfers data to the blocks. After a controller action has done its model interacting, it’s responsible for
Loading a layout object (which, indirectly, loads and creates block objects)
Tell that layout object to render a page.
Most Magento controller actions do this with two calls at the end of a controller action.
In Magento, nothing sets data on the view. Instead, the view (that is, the block objects) ask the system for data. You can see an example of this in the
Mage_Tag_Block_Customer_Viewblock class.Here, this block’s
getTagInfomethod asks the model directly for its information. This way, the front-end template developer has access to amethod. I also have it on good authority that a block’s
_prepareLayoutmethod is the perfect place to put most, if not all, of your data fetching code in a block.A second pattern you’ll see used is the Magento registry pattern. This is a Magento system that lets you set a system-wide (but not PHP) global variable.
Sometimes a Magento developer will use the registry to set a variable up in a controller action, and then grab is back out in the blocks. For example, in the admin console’s invoice controller.
and then a Block will reference it later.
I’m not wild about the registry pattern, but it’s used by the core team, so it’s probably kosher.
Finally, if you’re looking to emulate the “dumb view” pattern used in most PHP MVC frameworks, try something like this
and then in the block and/or template file.
After you call
loadLayout, Magento will have created all the block objects. What you’re doing above is getting reference to a specific block object, and then setting its data.Per Vinai’s comments below, there’s also a block’s
assignmethod to consider.Similar to
setData, after callingloadLayout(or from a block’s_prepareLayout) method, you can do something likeand then in your block’s
phtmlfile, you’d be able to output that view variable