0

如何在 Zend 框架中将 view.php 文件作为 json 对象获取?现在我在控制器中使用它:

// 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>
    )
)

这怎么可能?

4

1 回答 1

0

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

来自文档的示例

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

    // ...
}
于 2013-07-29T21:06:10.277 回答