获取View.php文件作为JSON对象


get View.php file as JSON object

如何在Zend框架中获得作为json对象的view.php文件?现在我在控制器中使用这个:

// in controller
public function helloAction()
{
    $this->view->meta->title = 'Hello';
    $this->view->meta->keywords = 'hello, wold, freebies';
    $this->view->meta->description = 'Blah blah blah';      
    $this->render();    
}
// in view *hello.php*
<div class="hello">Hello world</div>

我想在一个变量中捕获hello.php内容,比如这个

$html = $this->render();
$this->view->content = $html;

那么,使用这个?render=json我期待这样的结果

[view] => stdClass Object
(
    [meta] => stdClass Object
    (
        [title] => Hello
        [description] => hello, wold, freebies
        [keywords] => Blah blah blah
    )

    [content] => stdClass Object
    (   
        [view] => <div class="hello">Hello world</div>
    )
)

这怎么可能?

您可以使用zend框架上下文切换操作助手

文档中的示例

class NewsController extends Zend_Controller_Action
{
    public function init()
    {
        $contextSwitch = $this->_helper->getHelper('contextSwitch');
        $contextSwitch->addActionContext('list', 'xml')
                      ->initContext();
    }
    // ...
}