php Magento - 在控制器和块之间传递数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4006183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Magento - Passing data between a controller and a block
提问by Drew Hunter
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.
非常快速和简单的问题,但我找不到合适的答案 - 将数据从控制器传递到 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.
不确定这是否是最好的方法。
回答by Alan Storm
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.
在 Magento 的 MVC 方法中,为视图设置变量不是控制器的责任(在 Magento 的情况下,视图是布局和块)。控制器在模型上设置值,然后块从这些相同的模型中读取。在 Magento 的世界观中,让 Block 依赖控制器做特定的事情是紧密耦合的,应该避免。
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.
你的控制器的工作是对模型做某些事情,然后告诉系统它的布局渲染时间。就是这样。根据系统模型的状态以某种方式显示 HTML 页面是您的布局/块工作。
So, if I wanted to emulate traditional PHP MVC behaviors I'd
所以,如果我想模拟传统的 PHP MVC 行为,我会
Create a simple Model class the inherits from
Varien_Object
In the controller, instantiate that object using the
Mage::getSingleton('foo/bar')
Set values on the Model using the magic getter/setters (you get these in objects that inherit from
Varien_Object
), orsetData
, etc.In the Blocks, instantiate the Model again with a
Mage::getSingleton('foo/bar')
and read the values back.
创建一个简单的模型类继承自
Varien_Object
在控制器中,使用
Mage::getSingleton('foo/bar')
使用魔法 getter/setter 设置模型上的值(您可以在从 继承的对象中获取这些值
Varien_Object
),或setData
等。在块中,再次使用 a 实例化模型
Mage::getSingleton('foo/bar')
并读回值。
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.
当您使用Mage::getSingleton(...)
Magento实例化模型时,会将对象实例化为单例。因此,如果您重新实例化一个对象(再次使用Mage::getSingleton('foo/bar')
),您将返回同一个对象。
回答by Vinai
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()
如果您正在使用继承的块Mage_Core_Block_Template
(即使用模板来显示),您可以使用assign() 方法分配数据,一旦块被实例化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
然后,在 .phtml 模板中,您可以简单地使用
<?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.
这在 magento 中并不经常使用,但由于它是作为公共方法实现的,因此被声明为稳定的,我相信这样做是可以的。这也是约定使用下划线(例如$_product = $this->getProduct()
)开始在模板中声明的变量的原因,因此可以将它们与分配的变量区分开来。
回答by Josh Pennington
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();
回答by Jonathan Day
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.
您使用该Mage::registry()
方法走在正确的轨道上。另一种选择是使用自动 getter 和 setter,例如$this->setRandomVariableName($data)
在控制器中,然后在块中使用$this->getRandomVariableName()
。我没有调查它们是否最终位于堆栈中的相同位置(我假设在会话中它们是特定于请求的),但是它们在代码中实现了相同的目标。
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.
使用 getter 和 setter 有时会令人困惑,因为看起来您是通过 ORM 而不是“临时”会话变量访问数据,因此您可能会做出编码风格的一致性决定以Mage::registry
用于这些类型的变量。你的选择真的。
回答by Taras
You can use setData / getData pair for some values. I used setData in controller and getData in block.
您可以对某些值使用 setData / getData 对。我在控制器中使用了 setData,在块中使用了 getData。
回答by paderEpiktet
@Drew With some background in JavaServer Faces and rather new in PHP/Magento I would like to state that the
@Drew 在 JavaServer Faces 方面有一些背景,在 PHP/Magento 方面相当新,我想说的是
"'share nothing' architecture of PHP",
"'share nothing' 的 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".
请参阅PHP is not Java: Session Management Whitepaper",导致 PHP 中的所有对象(甚至类)都具有“请求”范围。
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.
- 一个“有状态”的模型对象,它的属性中有一些不一定存储在数据库中的数据
- 和单例模式,通过使用 Mage::getSingleton,使这个有状态模型在控制器中实例化,可访问块,因此在呈现输出的实际模板中。
And since a tool like MToolreduces the time to create a new model this really seems to make sense.
由于像MTool这样的工具减少了创建新模型的时间,这似乎很有意义。