2013-07-29 45 views
0

如何在Zend框架中將view.php文件作爲json對象?現在,我使用這個在控制器:將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; 

因此,使用這種渲染= JSON IM期待像這樣的結果

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

怎麼可能?

+0

你可能尋找http://php.net/manual/en/function.json-encode.php –

+0

context switch action helpers

例@DennisFischer不,我知道我可以將它解析爲json,但我需要這個對象。視圖正在生成「Hello world」,但我想將其保存在一個變量中。這樣我就可以將它解析爲json。 – jquery404

回答

0

您可以使用Zend框架從文檔

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

    // ... 
}