开发者

Magento - Passing data between a controller and a block

开发者 https://www.devze.com 2023-01-21 11:53 出处:网络
Really quick and simple question but I can\'t find a decent answer to this - What is the best way to pass data from a controller to a block in Magento.

Really quick and simple question but I can't find a decent answer to this - What is the best way to pass data from a controller to a block in Magento.

Incase it makes a difference, I am loading the layout as follows:

 $this->loadLayout(array('default', 'myModule_default'));

    $this->_initLayoutMessages('customer/session')
         ->_initLayoutMessages('catalog/session')
         ->renderLayout();

I should add, that I have been using the registry as开发者_JAVA百科 follows:

In the controller:

Mage::register('data', $data);

In the block:

$data = Mage::registry('data');

Not sure if this is the best way to do it though.


You don't.

In Magento's MVC approach, it's not the responsibility of the controller to set variables for the view (in Magento's case, the view is Layout and blocks). Controllers set values on Models, and then Blocks read from those same models. In Magento's view of the world, having a Block relying on the controller doing a specific thing is tight coupling, and should be avoided.

Your controller's job is to do certain things to Models, and then tell the system it's layout rendering time. That's it. It's your Layout/Blocks job to display an HTML page in a certain way depending on the state of the system's Models.

So, if I wanted to emulate traditional PHP MVC behaviors I'd

  1. Create a simple Model class the inherits from Varien_Object

  2. In the controller, instantiate that object using the Mage::getSingleton('foo/bar')

  3. Set values on the Model using the magic getter/setters (you get these in objects that inherit from Varien_Object), or setData, etc.

  4. In the Blocks, instantiate the Model again with a Mage::getSingleton('foo/bar') and read the values back.

When you instantiate a Model with Mage::getSingleton(...) Magento will instantiate the object as a singleton. So, if you re-instantiate an object (again with Mage::getSingleton('foo/bar')) you're getting back the same object.


If you are using blocks that inherit Mage_Core_Block_Template (i.e. that use a template to display) you can assign data using the assign() method, once the blocks have been instanciated by loadLayout()

$this->loadLayout(array('default', 'myModule_default'));

$this->getLayout()->getBlock('your.block.name.in.the.layout')->assign('data', $data);

Then, in the .phtml template, you can simply use

<?php echo $data ?>

This is not used very often in magento, but since it's implemented as public methods and thus declared stable, I believe it's fine do so. Thats also the reason for the convention to start variables declared in a template with an underscore (e.g. $_product = $this->getProduct()), so they can be distinguished from assigned variables.


What has worked for me in the is to set the variable in the controller by doing:

Mage::register('variable', 'value');

And then in the view you would retrieve the value using the following code:

$variable = $this->getVariable();


You're on the right track using the Mage::registry() approach. The other option is to use automatic getters and setters, e.g. $this->setRandomVariableName($data) in the controller and then in the block use $this->getRandomVariableName(). I haven't investigated whether they end up in the same location in the stack (I assume in the session as they are request-specific), but they achieve the same aim in the code.

Using the getters and setters can occasionally get confusing as it may look like you are accessing the data through the ORM rather than a "temporary" session variable, so you might make a coding-style consistency decision to use Mage::registry for those types of variables. Your choice really.


You can use setData / getData pair for some values. I used setData in controller and getData in block.


@Drew With some background in JavaServer Faces and rather new in PHP/Magento I would like to state that the

"'share nothing' architecture of PHP",

see PHP is not Java: Session Management Whitepaper", leads to the fact that all objects (and even classes) in PHP have the scope "request".

If I got Alans point then he advises to use

  • a 'stateful' model object that has some data in its attributes that is not necessarily stored in the database
  • and the singleton pattern, by the use of Mage::getSingleton, to make this stateful model, that is instantiated in the controller, accessable to the block and therefore in the actual template that renders the output.

And since a tool like MTool reduces the time to create a new model this really seems to make sense.

0

精彩评论

暂无评论...
验证码 换一张
取 消