如何在JSON响应中呈现ZF2视图? [英] How to render ZF2 view within JSON response?

查看:80
本文介绍了如何在JSON响应中呈现ZF2视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经弄清楚了如何在Zend Framework 2中返回典型的JSON响应.首先,我在view_manager配置的strategies部分中添加了ViewJsonStrategy.然后,我返回一个设置了所有变量的JsonModel实例,而不是从控制器操作中返回ViewModel实例.

So far, I have figured out how to return a typical JSON response in Zend Framework 2. First, I added the ViewJsonStrategy to the strategies section of the view_manager configuration. Then, instead of returning a ViewModel instance from the controller action, I return a JsonModel instance with all my variables set.

现在我已经弄清楚了这一点,我需要了解如何呈现视图并在该JSON响应中返回它.在ZF1中,我可以使用$this->view->render($scriptName),它以字符串形式返回HTML.在ZF2中,Zend\View\View::render(...)方法返回void.

Now that I've figured that piece out, I need to understand how to render a view and return it within that JSON response. In ZF1, I was able to use $this->view->render($scriptName), which returned the HTML as a string. In ZF2, the Zend\View\View::render(...) method returns void.

那么...如何呈现HTML视图脚本,并在一个请求中以JSON响应返回它?

So... how can I render an HTML view script and return it in a JSON response in one request?

这就是我现在拥有的:

    if ($this->getRequest()->isXmlHttpRequest()) {
        $jsonModel = new JsonModel(...);

        /* @todo Render HTML script into `$html` variable, and add to `JsonModel` */
        return $jsonModel;
    } else {
        return new ViewModel(...);
    }

推荐答案

好的,我想我终于明白了你在做什么.我找到了符合您条件的解决方案.尽管我确信仍有改进的余地,但仍有一些麻烦的工作要做...

OK, i think i finally understood what you're doing. I've found a solution that i think matches your criteria. Though i am sure that there is room for improvement, as there's some nasty handwork to be done...

public function indexAction()
{
  if (!$this->getRequest()->isXmlHttpRequest()) {
    return array();
  }

  $htmlViewPart = new ViewModel();
  $htmlViewPart->setTerminal(true)
               ->setTemplate('module/controller/action')
               ->setVariables(array(
                  'key' => 'value'
               ));

  $htmlOutput = $this->getServiceLocator()
                     ->get('viewrenderer')
                     ->render($htmlViewPart);

  $jsonModel = new JsonModel();
  $jsonModel->setVariables(array(
    'html' => $htmlOutput,
    'jsonVar1' => 'jsonVal2',
    'jsonArray' => array(1,2,3,4,5,6)
  ));

  return $jsonModel;
}

如您所见,我创建的templateMap令人讨厌……令人讨厌,我敢肯定它可以进行很多改进.这是一个可行的解决方案,但不是一个干净的解决方案.也许可以以某种方式从ServiceLocator中获取其可能已经实例化的默认PhpRenderer的模板和路径映射,然后它应该会更干净.

由于@DrBeza的评论,需要做的工作可以减少很多.现在,正如我最初想要的那样,我们将在所有模板映射完好无损的情况下获取viewrenderer,并直接渲染ViewModel.唯一重要的因素是您需要指定要渲染的完全合格的模板(例如:"$ module/$ controller/$ action")

Thanks to the comment ot @DrBeza the work needed to be done could be reduced by a fair amount. Now, as I'd initially wanted, we will grab the viewrenderer with all the template mapping intact and simply render the ViewModel directly. The only important factor is that you need to specify the fully qualified template to render (e.g.: "$module/$controller/$action")

我希望这会帮助您入门;)

I hope this will get you started though ;)

PS:响应如下:

Object:
    html: "<h1>Hello World</h1>"
    jsonArray: Array[6]
    jsonVar1: "jsonVal2"

这篇关于如何在JSON响应中呈现ZF2视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆