是否可以在Zend Framework中获取所有设置的视图变量?

Cod*_*ack 3 php zend-framework

如果我有一个视图并想要查看特定视图的所有设置变量,我该怎么做?

dre*_*010 9

分配给Zend_View对象的变量只是成为视图对象的公共属性.

以下是在特定视图对象中设置所有变量的几种方法.

在视图脚本中:

$viewVars = array();

foreach($this as $name => $value) {
    if (substr($name, 0, 1) == '_') continue; // protected or private

    $viewVars[$name] = $value;
}

// $viewVars now contains all view script variables
Run Code Online (Sandbox Code Playgroud)

Zend_View控制器中的对象:

$this->view->foo = 'test';
$this->view->bar = '1234';

$viewVars = get_object_vars($this->view);
// $viewVars now contains all public properties (view variables)
Run Code Online (Sandbox Code Playgroud)

最后一个示例对于手动创建的视图对象也同样有效 $view = new Zend_View();


Jea*_*ado 7

有一种更优雅的方式: $this->viewModel()->getCurrent()->getVariables();

对于嵌套的viewModel: $this->viewModel()->getCurrent()->getChildren()[0]->getVariables();