ZF2 在 phtml 中批量复制 ViewModel 变量
ZF2 Bulk copy ViewModel variables in phtml
在视图 phtml 文件中访问局部函数中的变量时,出现这个常见错误
Using $this when not in object context
我想知道有什么方法可以批量复制控制器中定义的 ViewModel 的变量容器数据结构中的所有变量和对象,作为要查看的局部变量和对象,以便我可以在局部函数中使用它们?
也就是说,像phtml中调用的一些CreateVariablesFromViewModelArray方法。因为我在 Zend Framework 2 的 ViewModel 变量容器数组中有很多数据。
听起来你有这样的东西:
//view + some html
<?php
function something() {
//do sth
};
?>
<p>some text</p>
<?php something() ?>
我推荐写一个view-helper。在那里你可以访问视图或者可以注入你需要的组件。
您可以将 $this 注入到您的函数中:
//view + some html
<?php
function something($view) {
//do sth
};
?>
<p>some text</p>
<?php something($this) ?>
- 不知道它是怎么叫的,但我相信这也是一个(不太好的)解决方案:
<?php
$something = function() use ($this) {
//do sth
};
?>
在视图 phtml 文件中访问局部函数中的变量时,出现这个常见错误
Using $this when not in object context
我想知道有什么方法可以批量复制控制器中定义的 ViewModel 的变量容器数据结构中的所有变量和对象,作为要查看的局部变量和对象,以便我可以在局部函数中使用它们?
也就是说,像phtml中调用的一些CreateVariablesFromViewModelArray方法。因为我在 Zend Framework 2 的 ViewModel 变量容器数组中有很多数据。
听起来你有这样的东西:
//view + some html
<?php
function something() {
//do sth
};
?>
<p>some text</p>
<?php something() ?>
我推荐写一个view-helper。在那里你可以访问视图或者可以注入你需要的组件。
您可以将 $this 注入到您的函数中:
//view + some html
<?php
function something($view) {
//do sth
};
?>
<p>some text</p>
<?php something($this) ?>
- 不知道它是怎么叫的,但我相信这也是一个(不太好的)解决方案:
<?php
$something = function() use ($this) {
//do sth
};
?>