Magento - 在控制器和块之间传递数据 [英] Magento - Passing data between a controller and a block

查看:20
本文介绍了Magento - 在控制器和块之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常快速和简单的问题,但我找不到合适的答案 - 将数据从控制器传递到 Magento 中的块的最佳方法是什么.

如果它有所不同,我将按如下方式加载布局:

 $this->loadLayout(array('default', 'myModule_default'));$this->_initLayoutMessages('客户/会话')->_initLayoutMessages('目录/会话')->renderLayout();

我应该补充一点,我一直在使用注册表,如下所示:

在控制器中:

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

在块中:

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

不确定这是否是最好的方法.

解决方案

你没有.

在 Magento 的 MVC 方法中,为视图设置变量不是控制器的责任(在 Magento 的情况下,视图是布局和块).控制器在模型上设置值,然后块从这些相同的模型中读取.在 Magento 的世界观中,让 Block 依赖控制器做特定的事情是紧密耦合的,应该避免.

你的控制器的工作是对模型做某些事情,然后告诉系统它的布局渲染时间.而已.根据系统模型的状态以某种方式显示 HTML 页面是您的布局/块工作.

所以,如果我想模仿传统的 PHP MVC 行为,我会

  1. 创建一个简单的模型类,继承自Varien_Object

  2. 在控制器中,使用 Mage::getSingleton('foo/bar')

  3. 实例化该对象
  4. 使用魔法 getter/setter(您在继承自 Varien_Object 的对象中获取这些值)或 setData 等在模型上设置值p>

  5. 在 Blocks 中,使用 Mage::getSingleton('foo/bar') 再次实例化模型并读取值.

当您使用 Mage::getSingleton(...) 实例化模型时,Magento 会将对象实例化为单例.因此,如果您重新实例化一个对象(再次使用 Mage::getSingleton('foo/bar')),您将返回相同的对象.

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 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.

这篇关于Magento - 在控制器和块之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆