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

时间:2013-07-29 21:03:02

标签: php zend-framework json

如何在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>
    )
)

怎么可能?

1 个答案:

答案 0 :(得分:0)

您可以使用zend框架context switch action helpers

docs中的示例

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

    // ...
}