Javascript 使用来自Zend Controller的数据为Angular JS渲染视图

Javascript 使用来自Zend Controller的数据为Angular JS渲染视图,javascript,php,angularjs,zend-framework,model-view-controller,Javascript,Php,Angularjs,Zend Framework,Model View Controller,我需要这个独特的需求,我的后端是PHP,Zend Framework采用MVC模式,前端是Angular JS,我需要将数据从后端传递到Angular JS控制器,同时呈现视图 据我所知,如果后端类继承Zend_Controller_Action,它只返回View而不返回data;如果后端类继承Zend_Rest_Controller,它只返回data而不返回View 这是正确的吗?有没有一种方法可以在不使用$this->view->data概念的情况下呈现视图并返回数据。要返回JSON数据,您

我需要这个独特的需求,我的后端是PHP,Zend Framework采用MVC模式,前端是Angular JS,我需要将数据从后端传递到Angular JS控制器,同时呈现视图

据我所知,如果后端类继承Zend_Controller_Action,它只返回View而不返回data;如果后端类继承Zend_Rest_Controller,它只返回data而不返回View


这是正确的吗?有没有一种方法可以在不使用$this->view->data概念的情况下呈现视图并返回数据。

要返回JSON数据,您只需要

这个助手将设置适当的头并简单地返回一个JSON对象。如果您的操作中有一些错误处理,并且需要返回除200OK以外的代码,那么您还可以获取响应对象$this->getResponse并设置这些值


还有其他变体,您可以将layout设置为noRender和Context,但JSON助手已经处理了所有这些。

如果您将angularJs用于前端,那么zendframework将仅用于后端服务,例如:CRUD操作。您必须同时处理框架Angularjs和Zendframework的路由。我认为您需要没有视图的数据。如果您使用angularjs作为前端,zend framework作为后端作为Web服务。是否希望从zend返回JSON数据?@adrian-是的,我需要从zend Controller返回JSON数据,同时呈现视图。
public function someAction() {
    // create an array with your data
    $data = array();

    // to add some view data
    $data['html'] = $this->view->render('/path/to/script/script-name.phtml');
    // essentially you can load and render any script and I am
    // not 100% sure but without a path it would render the default
    // action script in view/scripts/controller-name/action-name.phtml

    // Send the JSON response. Both methods do the same
    $this->_helper->json($data);
    $this->_helper->json->sendJson($data);

    // NOTE both methods terminate and return the data
    // i.e. the follow should not been seen
    echo "This should not be seen!";
}