More than 1000+ satisfied clients and counting, since 2010

Blog

Pass Data Between Magento Controller and Block

Pass Data Between Magento Controller and BlockThe working mechanism of Magento Controllers and Blocks are way too different. So inorder to pass data between Magento Controller and Block, it is necessary to understand how they work together. Briefly, the Controller’s job is to carry out the task of Models and inform the system about the layout’s rendering time. While Blocks need to display HTML pages depending on the state of the system models.

How to Pass Data Between Magento Controller and Block

There are a number of ways to pass data between Magento Controller and Block. This blog post covers one such method. Follow the steps given below to pass data between Magento Controller and Block easily.

In case of Magento content management system, the view is layout and blocks only. So controllers are not responsible for setting variables for the view in the MVC approach. Controllers only set the values on models while Blocks read from those models.

So to completely relying on a mechanism to pass data between Magento Controller and Block directly is not recommended. It is because of the tight coupling involving the two.

  • Create a simple Model class from Varien_Object
  • Instantiate the object in the Controller using, Mage::getSingleton(‘foo/bar’)
  • Set values on the Model
  • In Blocks, instantiate the Model again with Mage::getSingleton(‘foo/bar’)

The second time it is instantiated to read the values back. Magento instantiates the object as a singleton. So each time the object is instantiated, the same object is returned. This is the best method to pass data between Magento Controller and Block.

Written by

The author didnt add any Information to his profile yet