【问题标题】:Rendering View with Data from Zend Controller for Angular JS使用来自 Angular JS 的 Zend 控制器的数据渲染视图
【发布时间】:2015-11-26 16:19:17
【问题描述】:

我需要这个独特的要求,我的后端是PHPMVC 模式中的Zend Framework,前端是Angular JS,我需要将数据从后端传递到 Angular JS 控制器并渲染同时查看。

据我所知,如果后端类继承Zend_Controller_Action,它只返回视图而不返回数据,如果后端类继承Zend_Rest_Controller,它只返回数据而不返回视图。

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

【问题讨论】:

  • 如果您将 angularJs 用于前端,那么 zendframework 将仅用于后端服务(例如:CRUD 操作)。你必须同时使用框架 Angularjs 和 Zendframework 的路由。
  • 我认为您需要没有视图的数据。如果您使用angularjs 作为前端,zend framework 作为后端作为 Web 服务。
  • 你想要从 Zend 返回的 JSON 数据吗?
  • @adrian - 是的,我需要从 Zend 控制器和视图返回的 JSON 数据同时呈现。

标签: javascript php angularjs zend-framework model-view-controller


【解决方案1】:

要返回 JSON 数据,您只需要JSON Action Helper

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!";
}

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

还有其他变体,您可以将布局设置为 noRender 和上下文,但这些都已由 JSON 帮助程序处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 2015-10-19
    • 2011-06-18
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多